Test instructions: Give you a tree that allows you to find the farthest distance from each point in the tree.
... Will not do, think for a long day to check the puzzle.
The farthest distance from each point is either to go down or to come down from the parent node.
Dp[u][0] Indicates the maximum number of runs of a son's road, Dp[u][1] represents the second largest value of a running son's road. Why do you want to record the second big value?
DP[U][2] Indicates the maximum number of runs toward the father.
DP[V][2] In common sense should be equal to Max (dp[u][0],dp[u][2]) +w[u][v]. But dp[u][0] may have been obtained from dp[v][0, as in that case dp[u][0] is to run to V's there. In this case, Max (dp[u][1],dp[u][2]) +w[u][v] will be used;
#include <iostream>#include<cstdio>#include<cstring>#include<ctime>#include<algorithm>#include<cstdlib>#include<cmath>#include<map>#include<vector>#include<Set>using namespaceStd;typedefLong Longll;typedef unsignedLong LongUll;typedefDoubledb;#defineX First#defineY Second#defineMP (A, b) Make_pair (A, B)#definePB Push_back#defineSD (x) scanf ("%d",& (x))#definePi ACOs (-1.0)#defineSF (x) scanf ("%lf",& (x))#defineSS (x) scanf ("%s", (x))#defineMAXN 10005Const intinf=0x3f3f3f3f;Constll mod=1000000007;structnode{intV,w,next;} E[MAXN];inttop;intHEAD[MAXN];voidAddintUintVintW) {E[TOP].V=v; E[TOP].W=W; E[top].next=Head[u]; Head[u]=top++;}intdp[maxn][3];voidDfsintu) { intmm=0, sm=0, V,pa; for(inti=head[u];i!=-1; i=E[i].next) {v=e[i].v; DFS (v); PA=dp[v][0]+E[I].W; if(pa>=mm) {SM=mm; MM=PA; } Else if(pa>=sm) SM=PA; } dp[u][0]=mm; dp[u][1]=SM;}voidDFS2 (intu) { intv; for(inti=head[u];i!=-1; i=E[i].next) {v=e[i].v; dp[v][2]=max (dp[u][2],dp[v][0]+e[i].w==dp[u][0]?dp[u][1]:d p[u][0])+E[I].W; DFS2 (v); }}intMain () {#ifdef local freopen ("inch","R", stdin); //freopen ("Out", "w", stdout); int_time=clock ();#endif intN; while(cin>>N) {Top=0; memset (Head,-1,sizeofhead); for(intv=2; v<=n;v++) { intu,w; scanf ("%d%d",&u,&W); Add (U,V,W); } DFS (1); dp[1][2]=0; DFS2 (1); for(intI=1; i<=n;i++) {printf ("%d\n", Max (dp[i][0],dp[i][2])); }} #ifdef local printf ("Time :%d\n",int(Clock ()-_time));#endif}View Code
Hdu 2196 Computer