Hdu2196 computer tree DP

Source: Internet
Author: User
This question has a very classic algorithm. Search twice. Record the maximum two values of each node for the first time, record the source of the maximum value, and the second search shows that each node can reach the maximum value after passing through the father's day. Finally, find the maximum value from the value recorded above, that is, the farthest transmission distance of each node we are looking for in the question.

We recommend that you go to an article "Analysis of ideas and methods for solving problems using tree-based Dynamic Planning", which describes the source idea of the maximum value.

Computer

Time Limit: 1000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 1112 accepted submission (s): 538

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 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.

Inputinput 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.

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
 
# Include <iostream> # include <vector> # include <queue> # define maxn 10100 // tree-like DP classic using namespace STD; int N; bool visited [maxn]; int DP [3] [maxn]; // [0] maximum [1] times large [2] goes through the maximum int longfrom [maxn] of the Father's Day point; Class adj {public: int DIS; int node ;}; typedef vector <adj> Computer; Computer C [maxn]; int max (int A, int B) {return A> B? A: B;} void Init () {memset (visited, 0, sizeof (visited); memset (DP, 0, sizeof (DP); memset (longfrom, 0, sizeof (longfrom);} void dp1 (INT root) // The first DFS search, stores the maximum value and secondary value of each node, and records the source of the maximum value {If (visited [root]) return; visited [root] = true; DP [0] [root] = 0; DP [1] [root] = 0; DP [2] [root] = 0; longfrom [root] = 0; For (INT I = 0; I <C [root]. size (); I ++) {int NO = C [root] [I]. node; If (visited [No]) continue; dp1 (NO ); if (DP [0] [root] <DP [0] [No] + C [root] [I]. DIS) {DP [1] [root] = DP [0] [root]; // longfrom [root] = no; // record the maximum value of the source DP [0] [root] = DP [0] [No] + C [root] [I]. DIS; // Update maximum value} else {If (DP [1] [root] <DP [0] [No] + C [root] [I]. dis) // update DP [1] [root] = DP [0] [No] + C [root] [I]. dis ;}} void dp2 (INT root) // BFs. BFS is used here. It can also be used with DFS. It looks more efficient {queue <int> ns; if (visited [root]) return; NS. push (Root); While (! NS. empty () {int nn = NS. size (); For (INT I = 0; I <nn; I ++) {int temp = NS. front (); NS. pop (); For (Int J = 0; j <C [temp]. size (); j ++) {int NO = C [temp] [J]. node; If (! Visited [No]) ns. Push (NO); elsecontinue; If (No! = Longfrom [temp]) // source for determining the maximum value of Father's Day: DP [2] [No] = max (DP [2] [temp], DP [0] [temp]) + C [temp] [J]. DIS; // The maximum value of Father's Day does not pass through the current node, then select the parent node and click the maximum value to update the current node. The maximum value of elsedp passed through the parent node is elsedp [2] [No] = max (DP [2] [temp], DP [1] [temp]). + C [temp] [J]. DIS; // otherwise Select secondary update} visited [temp] = true ;}} int main () {While (CIN> N) {Init (); for (INT I = 0; I <= N; I ++) C [I]. clear (); For (INT I = 2; I <= N; I ++) {int to, l; adj news; CIN> to> L; news. node = I; news. dis = L; C [to]. push_back (News); news. node = to; C [I]. push_back (News);} dp1 (1); // One-time memset (visited, 0, sizeof (visited); dp2 (1 ); // update the maximum value of the secondary search for (INT I = 1; I <= N; I ++) {cout <max (DP [1] [I], max (DP [0] [I], DP [2] [I]) <Endl; // select the maximum of three values for each node }}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.