求助莫名错误
查看原帖
求助莫名错误
231800
Veranda楼主2023/11/12 21:41

求助莫名错误,RT,感觉做法没问题,但是只能过第一个点

(或者提供一组hack样例也行)

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;

const LL N = 500010;
const LL M = 2 * N;

LL n,s,maxd;
LL h[N],e[M],ne[M],w[M],idx;
LL d[N],fa[N];
LL f[N];

void add(LL a,LL b,LL c){
    e[idx] = b,w[idx] = c,ne[idx] = h[a],h[a] = idx ++;
}

void dfs(LL u,LL father,LL dep){
    //cout << u << " " << father << " " << dep << endl;
    
    fa[u] = father;
    d[u] = dep;
    maxd = max(maxd,dep);
    
    for(LL i = h[u];~i;i = ne[i]){
        LL j = e[i],val = w[i];
        if(j == father) continue;
        
        dfs(j,u,dep + val);
    }
}

void dp(LL u,LL father){
    LL t = 0x3f3f3f3f;
    bool f = 0;
    
    for(LL i = h[u];~i;i = ne[i]){
        LL j = e[i];
        if(j == father) continue;
        
        f = 1;
        t = min(d[j],t);
        
        dp(j,u);
        
    }
    if(f){
        d[u] += t;
        for(LL i = h[u];~i;i = ne[i]){
            LL j = e[i];
            if(j == father) continue;
            d[j] -= t;
        }
    }
}

void work(LL u,LL father){
    bool f = 0;
    for(LL i = h[u];~i;i = ne[i]){
        LL j = e[i];
        if(j == father) continue;
        
        f = 1;
        work(j,u);
    }
    if(f){
        d[u] = 0;
    }
    else d[u] = maxd - d[u];
}

int main(){
    memset(h,-1,sizeof h);
    
    cin >> n >> s;
    for(LL i = 1;i < n;i ++){
        LL a,b,t;
        cin >> a >> b >> t;
        add(a,b,t);add(b,a,t);
    }
    
    dfs(s,-1,0);
    
    work(s,-1);
    
    //cout << maxd;
    
    //for(LL i = 1;i <= n;i ++ ) cout << d[i] << " ";
    //cout << endl;
    
    dp(s,-1);
    
    LL res = 0;
    for(LL i = 1;i <= n;i ++) res += d[i];
    
    cout << res;
}
2023/11/12 21:41
加载中...