wordpress更换网站域名,在线式crm,免费加入微商代理,龙岩做网站龙岩网页设计Portal.
先找出树上以 S S S 为起点最长的一条链#xff0c;然后让其他链的长度都和该链对齐即可。 维护每个结点 x x x 的子树最长链 d max ( x ) d_{\max}(x) dmax(x)#xff0c;则每次 DFS 求出最长链之后调整对齐的代价为 d max ( x ) − ( d max ( s o …Portal.
先找出树上以 S S S 为起点最长的一条链然后让其他链的长度都和该链对齐即可。 维护每个结点 x x x 的子树最长链 d max ( x ) d_{\max}(x) dmax(x)则每次 DFS 求出最长链之后调整对齐的代价为 d max ( x ) − ( d max ( s o n x ) w i ) d_{\max}(x)-(d_{\max}(son_x)w_i) dmax(x)−(dmax(sonx)wi)。
#include bits/stdc.h
using namespace std;
#define int long longconst int maxn5e55;
int head[maxn],V,cnt,mxd[maxn];
struct edge{int to,nxt,w;}e[maxn];void add(int x,int y,int z){e[cnt](edge){y,head[x],z},head[x]cnt;}void dfs(int x,int fa)
{for(int ihead[x];i;ie[i].nxt){if(e[i].tofa) continue;dfs(e[i].to,x),mxd[x]max(mxd[x],mxd[e[i].to]e[i].w);}for(int ihead[x];i;ie[i].nxt){if(e[i].tofa) continue;Vmxd[x]-(mxd[e[i].to]e[i].w);}
}signed main()
{int N,S;cinNS;for(int i1,a,b,t;iN;i) cinabt,add(a,b,t),add(b,a,t);dfs(S,0);coutV;return 0;
}