hdu2196 computer (diameter of the tree | | The longest path in the tree)

Source: Internet
Author: User

Computer Time limit:1000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 5232 Accepted Submission (s): 2640


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 Input
51 12 13) 11 1

Sample Output
32344

Authorscnu
Recommendlcy | We have carefully selected several similar problems for you:1561 1011 3456 1520 2242
Statistic | Submit | Discuss | Note


If you want to do this problem first understand the tree's longest road how to calculate

first assume that the tree's longest two leaf nodes are v1,v2, then The existing conclusion that the farthest point from any point u must be (V1,V2), and then

The furthest point from V1 or V2 must be v2 or v1. So after two searches you can find the longest path.

Here are the proofs:

Suppose s-t This path is the diameter of a tree, or the longest road on a tree

The existing conclusion, from any point U search to the farthest point must be s, T, and then at the farthest point from the search, you can search the end of another longest road, that is, with two times wide search can find the longest road of the tree

Prove:

1 set U as a point on the S-T path, the conclusion is clearly established, otherwise the farthest point to be searched is T

Dis (u,t) >dis (u,s) and Dis (u,t) >dis (u,t) The longest road is not s-t, with the assumption contradiction

2 set U is not a point on the s-t path

First of all, if you go to a point on the s-t path, then the next path must be on the s-t, and the end point is s or T, which has been proven in 1.

So now there are two different things:

1:u goes to a point on the s-t path, assuming X, and finally going to an endpoint, assuming T, the total path length is dis (u,x) +dis (x,t)

2:u go to the farthest point of the path u-t and s-t without intersections, then dis (u-t) >dis (u,x) +dis (x,t); Obviously, if this formula is established,

Then dis (u,t) +dis (s,x) +dis (u,x) >dis (s,x) +dis (x,t) =dis (s,t) The longest road is not s-t contradiction

Proof of transfer from: http://www.cnblogs.com/wuyiqi/archive/2012/04/08/2437424.html

The problem is not to find the longest path, is to find a node x can reach the longest path. First, the longest path of this node x is either the path to the V1 or the path to v2. Because we don't know where to go is the longest path, so we need to search from V1,v2 point for a total of three searches in this case, maybe some of the homecoming students said two times. We found a point in the v1,v2 in the first search and then from the V1 or V2 point to another point v2 or v1 in the process of searching by dist[x] Save the path length of the last output by comparing the current path and the longest path minus the current path to the maximum (max ( DIST[V]-DIST[X],DIST[X]) In fact, I am also a part of these students to submit the time WA, after careful analysis of this is wrong we can only guarantee that x go to the farthest point is a bit of V1, or V2 can not guarantee that another point is also so can not be subtracted. Look at this picture.


You can see the longest path is 7-"1-" 4-"5-" 6-"8 dist[7]=dist[8]=5

First time search from 1 get dist[8]=5,dist[2]=3

If according to this formula (Max (dist[8]-dist[2],dist[2)) =3 But in the figure we can see clearly dist[2] The maximum value is 2-"5-" 4-"1-" 7 length 4


That's all you have to say. Attached code:

#include <stdio.h> #include <vector> #include <algorithm> #include <string.h>using namespace    Std;int root,max_len;int dist[10000+5];struct edge{int pos;    int cost;        Edge (int a,int x) {pos=a;    Cost=x;    }};vector<edge>tree[10000+5];void dfs (int u,int v,int len) {if (Max_len<len) max_len=len,root=u;        for (int i=0;i<tree[u].size (); i++) {int pos=tree[u][i].pos;        int cost=tree[u][i].cost;        if (V==pos) continue;        DFS (Pos,u,len+cost);    Dist[pos]=max (Dist[pos],len+cost);    }}int Main () {int n;        while (~SCANF ("%d", &n)) {int max_val=0;        memset (tree,0,sizeof (tree));        memset (dist,0,sizeof (Dist));            for (int i=2;i<=n;i++) {int a,x;            scanf ("%d%d", &a,&x);            Tree[i].push_back (Edge (a,x));        Tree[a].push_back (Edge (i,x));        } max_len=0;        DFS (1,-1,0);        DFS (root,-1,0);    DFS (root,-1,0);    for (int i=1;i<=n;i++) printf ("%d\n", Dist[i]);  } return 0;}


hdu2196 computer (diameter of the tree | | The longest path in the tree)

Related Article

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.