HDU 2196 Computer-from lanshui_Yang

Source: Internet
Author: User

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
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
5
1 1
2 1
3 1
1 1

Sample Output
3
2
3
4
4. abstract questions: give you a tree to find the maximum distance between any node in the tree and other nodes. Solution: the simple idea is to call bfs once for each node to sum the maximum distance of other nodes in the tree. However, it is obvious that it uses TLE. In another way, first obtain the two endpoints A and B of the tree's diameter. Then, the maximum value of node C in the tree from other nodes must be max {A-> C, b-> C}. In this way, you only need to call bfs three times. For the first time, use any node as the starting point to find the bfs endpoint, the second time, we start with endpoint A for bfs to find endpoint B (in this process, we also find the distance from other nodes in the tree to endpoint ), the third time is to start with endpoint B for bfs. This process is to obtain the distance from other nodes in the tree to endpoint B while finding max {A-> C, b-> C }. See the code below:


# Include <iostream> # include <cstring> # include <string> # include <algorithm> # include <cstdio> # include <queue> using namespace std; const int MAXN = 1e6 + 7; int n; struct Node {int adj; int dist; Node * next ;}; Node * vert [MAXN]; int vis [MAXN]; // create A tag array int distmp [MAXN]; // record the distance from endpoint A to other nodes in the tree int disans [MAXN]; // record max {A-> C, b-> C} queue <int> q; int bfs (int start) {memset (vis, 0, sizeof (vis); // bf each time S do not forget memset (distmp, 0, sizeof (distmp); while (! Q. empty () {q. pop () ;}int duan; vis [start] = 1; distmp [start] = 0; if (distmp [start]> disans [start]) // find max {A-> C, B-> C} {disans [start] = distmp [start];} q. push (start); int maxr = 0; Node * p; int tmp; while (! Q. empty () {tmp = q. front (); q. pop (); vis [tmp] = 1; p = vert [tmp]; while (p! = NULL) {int tp2 = p-> adj; if (! Vis [tp2]) {vis [tp2] = 1; distmp [tp2] = distmp [tmp] + p-> dist; if (distmp [tp2]> disans [tp2]) {disans [tp2] = distmp [tp2];} if (maxr <distmp [tp2]) {duan = tp2; maxr = distmp [tp2];} q. push (tp2);} p = p-> next;} return duan;} void ans () // perform 3 bfs {int duan1 = bfs (1 ); int duan2 = bfs (duan1); bfs (duan2);} int main () {while (scanf ("% d", & n )! = EOF) {memset (disans, 0, sizeof (disans); memset (vert, 0, sizeof (vert); int I; getchar (); for (I = 2; I <= n; I ++) // creates a graph. It is very important to understand the meaning of the question !! {Int B, d; scanf ("% d", & B, & d); Node * p; p = new Node; p-> adj = B; p-> dist = d; p-> next = vert [I]; vert [I] = p; p = new Node; p-> adj = I; p-> dist = d; p-> next = vert [B]; vert [B] = p;} ans (); for (I = 1; I <= n; I ++) {printf ("% d \ n", disans [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.