HDU 2196 computer (farthest distance of nodes on tree)

Source: Internet
Author: User

Computer

Time limit:1000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 4080 Accepted Submission (s): 2043


Problem Descriptiona School bought the first computer some time ago (so this computer ' s ID is 1). During The recent years the school bought N-1 new computers. Each new computer is connected to one of settled earlier. Managers of school is anxious about slow functioning of the net and want to know the maximum distance Si for which i-th C Omputer needs to send signal (i.e. length of cable-the most distant computer). You need to provide this information.


hint:the example input is corresponding to this graph. And from the graph, you can see that the computer 4 are farthest one from 1, so S1 = 3. Computer 4 and 5 is the farthest ones from 2, so S2 = 2. Computer 5 is the farthest one from 3, so S3 = 3. We also get S4 = 4, S5 = 4.

Inputinput file contains multiple test cases. In each case there was natural number N (n<=10000) in the first line, followed by (N-1) lines with descriptions of Compu Ters. I-th line contains-natural numbers-number of computer, to which i-th computer is connected and length of cable used for connection. Total length of cable does not exceed 10^9. Numbers in lines of input is separated by a space.

Outputfor each case output N lines. I-th line must contain number Si for i-th computer (1<=i<=n).

Sample Input51 12 13 11 1

Sample Output32344 Classic Problem, the first DP to handle the longest sub-tree, the second long distance (there is one side [u, V, W], U's Subfar may be the farthest path son v farthest (U's Subfar + W)) ~ ~ To solve the time to consider that temp is from the parent The longest route on a pro path, and then divide your son into whether it is the farthest way to solve the answer ~
#include <bits/stdc++.h>using namespacestd;Const intN =10010 ;intdp[n][2], son[n][2], ans[n], N;intEh[n], et[n<<1], nxt[n<<1], ew[n<<1], tot;voidinit () {memset (Eh,-1,sizeofeh); Tot=0 ;} voidAddedge (intU,intVintW) {Et[tot]= V; Ew[tot] = W; Nxt[tot] = Eh[u]; Eh[u] = tot++ ; Et[tot]= u; Ew[tot] = W; Nxt[tot] = Eh[v]; EH[V] = tot++ ;}intDp (intU,intFA) {     for(inti = Eh[u]; ~i; i =Nxt[i]) {        intv = et[i], W =Ew[i]; if(v = = FA)Continue ; intTMP = Dp (V, u) +W; if(tmp > dp[u][1]) {dp[u][1] =tmp; son[u][1] =v; }        if(dp[u][1] > dp[u][0]) {Swap (dp[u][1], dp[u][0] ) ; Swap (son[u][1], son[u][0]) ; }    }    returndp[u][0] ;}voidSolve (intU,intFA,inttmp) {Ans[u]= Max (dp[u][0], TMP);  for(inti = Eh[u]; ~i; i =Nxt[i]) {        intv = et[i], W =Ew[i]; if(v = = FA)Continue ; if(v = = son[u][0]) {Solve (V, u, max (dp[u][1], TMP) +W); } Else{Solve (V, u, max (dp[u][0], TMP) +W); }    }}intMain () { while(~SCANF ("%d",&N)) {init ();  for(inti =2; I <= N; ++i) {intV, W; scanf"%d%d",&v,&W);         Addedge (i, V, W); } memset (DP,0,sizeofDP); Dp (1,0 ); //for (int i = 1; I <= n; ++i) cout << i << ' << dp[i][0] << ' << dp[i][1] <& Lt Endl;Solve (1,0,0 );  for(inti =1; I <= N; ++i) printf ("%d\n", Ans[i]); }    return 0 ;}
View Code

HDU 2196 computer (farthest distance of nodes on tree)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.