vjudge
智商掉线...
可以发现一条边能贡献其他点当且仅当两点路径上这个边权值最小,所以如果按照边权从大到小加边,每加一条边就会合并两个联通块,那么一个联通块内的点到另一个联通块的点的权值就都是那条边的边权,所以可以给两个联通块内的点答案分别加上边权\(*\)另一个联通块点数.然后这个可以用类似重构树的方法维护,具体是每次给两个联通块根节点打标记,然后查询某个点就是根到这个点路径上的标记和
#include<bits/stdc++.h>
#define LL long long
#define uLL unsigned long long
#define db doubleusing namespace std;
const int N=2e5+10;
int rd()
{int x=0,w=1;char ch=0;while(ch<'0'||ch>'9'){if(ch=='-') w=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}return x*w;
}
struct node{int x;LL y;};
struct edge
{int x,y,z;bool operator < (const edge &bb) const {return z>bb.z;}
}e[N];
int n,pt,ff[N<<1],sz[N<<1],ch[N<<1][2];
LL tg[N<<1],ans;
int findf(int x){return ff[x]==x?x:ff[x]=findf(ff[x]);}
queue<node> q;int main()
{while(~scanf("%d",&n)){for(int i=1;i<=n;++i)ff[i]=i,sz[i]=1;for(int i=1;i<=n+n;++i) tg[i]=ch[i][0]=ch[i][1]=0;for(int i=1;i<n;++i){int x=rd(),y=rd(),z=rd();e[i]=(edge){x,y,z};}sort(e+1,e+n);pt=n;for(int i=1;i<n;++i){int x=findf(e[i].x),y=findf(e[i].y),z=e[i].z;tg[x]+=1ll*sz[y]*z,tg[y]+=1ll*sz[x]*z;++pt,ff[x]=ff[y]=ff[pt]=pt,sz[pt]=sz[x]+sz[y],ch[pt][0]=x,ch[pt][1]=y;}ans=0;q.push((node){pt,0});while(!q.empty()){int x=q.front().x;LL di=q.front().y;q.pop();ans=max(ans,di);if(!x) continue;q.push((node){ch[x][0],di+tg[ch[x][0]]});q.push((node){ch[x][1],di+tg[ch[x][1]]});}printf("%lld\n",ans);}return 0;
}