Meaning of the questions:
Original lab with a computer (No. 1), DD is the name of the nearest green and laboratory py sponsorship, the purchase of computers N-1, No. 2 to N. Every computer connected to a computer with a previously installed cable. But the squad Gangster worry speed is too slow, i asked him the first computer to a maximum cable length of other computers, but the fun of the name DD addicted to green the city skyline forgot to calculate, please help him.
answer:
nmd too hard, I will kneel
#include<cstdio> #include<algorithm> #include<cstring> using namespace std; const int maxn=1e4+100; struct node { int u,v,w,next; }edge[maxn*2]; int tol; int head[maxn*2]; void addedge (int u,int v,int w) { edge [tol] .u = u; edge [tol] .v = v; edge[tol].w=w; edge[tol].next=head[u]; head[u]=tol++; } int DP [MAXN] [ . 3 ]; // are the maximum positive, large positive times, the maximum reverse int L [MAXN]; int DFS ( int u, int pre) { // return the forward maximum distance u IF (DP [U] [ 0 ]> = 0 ) return DP [U] [ 0 ]; dp[u][0]=dp[u][1]=dp[u][2]=l[u]=0; for (int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].v; if (v==pre) continue; if (dp[u][0]<dfs(v,u)+edge[i].w) { l[u]=v; dp[u][1]=max(dp[u][1],dp[u][0]); dp[u][0]=dfs(v,u)+edge[i].w; } else if (dp[u][1]<dfs(v,u)+edge[i].w) dp[u][1]=max(dp[u][1],dfs(v,u)+edge[i].w); } return dp[u][0]; } void dfs_again (int u,int pre) { for (int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].v; if (v==pre) continue; if (v==l[u]) dp[v][2]=max(dp[u][2],dp[u][1])+edge[i].w; else dp[v][2]=max(dp[u][2],dp[u][0])+edge[i].w; dfs_again(v,u); } } int main () { int N; while (~scanf("%d",&N)) { toll = 0 ; memset(head,-1,sizeof(head)); memset(dp,-1,sizeof(dp)); memset(l,-1,sizeof(l)); for (int i=2;i<=N;i++) { int v,w; scanf("%d%d",&v,&w); addedge(i,v,w); addedge(v,i,w); } dfs(1,-1); dfs_again(1,-1); for (int i=1;i<=N;i++) printf("%d\n",max(dp[i][0],dp[i][2])); } }