Hdu2196 Computer (tree dp)

Source: Internet
Author: User

Hdu2196 Computer (tree dp)
ComputerTime Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 4715 Accepted Submission (s): 2376



Problem Description A 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 was connected to one of settled earlier. managers of school are anxious about slow functioning of the net and want to know the maximum distance Si for which I-th computer needs to send signal (I. e. length of cable to 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 is farthest one from 1, so S1 = 3. computer 4 and 5 are 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.
Input file contains multiple test cases. in each case there is natural number N (N <= 10000) in the first line, followed by (N-1) lines with descriptions of computers. i-th line contains two 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 are separated by a space.
Output For each case output N lines. I-th line must contain number Si for I-th computer (1 <= I <= N ).
Sample Input
51 12 13 11 1

Sample Output
32344 question: Given a tree, each side of the tree has a value. How much is the maximum sum of the obtained path weights starting from each vertex? Each tree edge can only go once. Analysis: Find the longest sub-node chain with each node as the root, and add this node as another sub-tree chain of the child. Detailed analysis
# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
# Include
      # Include
       
         # Include
        
          # Include
         
           # Include using namespace std; const double eps = 1e-6; const double pi = acos (-1.0); const int INF = 0x3f3f3f; const int MOD = 1000000007; # define ll long # define CL (a, B) memset (a, B, sizeof (a) # define MAXN 10005 struct node {int v, len, sum; node * next;} tree [MAXN * 2], * head [MAXN * 2]; int n, ptr, vis [MAXN]; int dp [MAXN]; void init () {ptr = 1; CL (vis, 0); CL (dp, 0); CL (head, NULL);} void add (int s, Int e, int len) // build {tree [ptr]. v = e; tree [ptr]. len = len; tree [ptr]. next = head [s]; head [s] = & tree [ptr ++]; tree [ptr]. v = s; tree [ptr]. len = len; tree [ptr]. next = head [e], head [e] = & tree [ptr ++];} void dfs1 (int idx) // The longest chain {vis [idx] = 1; node * p = head [idx]; while (p! = NULL) {if (! Vis [p-> v]) {dfs1 (p-> v); dp [idx] = max (dp [idx], dp [p-> v] + p-> len); p-> sum = dp [p-> v] + p-> len ;} p = p-> next ;}} void dfs2 (int father, int child) // another subtree with the same root {if (vis [child]) return; vis [child] = 1; int maxx = 0; node * p = head [father]; while (p! = NULL) // find the maximum value of other branches except child on the parent node {if (p-> v! = Child) maxx = max (maxx, p-> sum); p = p-> next;} p = head [child]; while (p! = NULL) // continue to update to ensure that the optimal solution is obtained in each step {if (p-> v = father) {p-> sum = p-> len + maxx; break;} p = p-> next;} p = head [child]; while (p! = NULL) // update the current node each time and perform recursive calculations. The parent node does not calculate {dp [child] = max (dp [child], because vis = 1. p-> sum); dfs2 (child, p-> v); p = p-> next ;}} int main () {int a, B; while (scanf (% d, & n) = 1) {init (); for (int I = 2; I <= n; I ++) {scanf (% d, & a, & B); add (I, a, B) ;}dfs1 (1); CL (vis, 0 ); node * p = head [1]; while (p! = NULL) {dfs2 (1, p-> v); p = p-> next;} for (int I = 1; I <= n; I ++) printf (% d, dp [I]);} return 0 ;}
         
        
       
     
    
   
  
 


 

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.