HDU 2196 Computer Tree DP

Source: Internet
Author: User

Computer

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


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 1 Sample Output32344 has a computer-made, tree-like network that asks for the maximum distance from each computer to other computers. There were 1th computers, after which no computer was connected to the original computer, and there was a distance of c. The data in the problem is large, so it is not possible to recursively seek depth for each node. Think about it, the maximum distance for each node is from its parent node or its child nodes, with the number 1th computer as the root node. The situation from the child nodes is relatively simple, we just need to find the depth of the line maxn[t] = max (Maxn[ti] + L[TI][FA]). And the parent node is divided into circumstances, for the T node, the parent node is the FA node, if the maximum distance of the FA node is not through the T node, then the maximum distance of the T node is Max (Maxn[t], MAXN[FA] + L[T][FA]); if it is through the T-node, That can still come from the FA node, but the path is not the maximum distance that is the second big distance that the--SECOND_MAXN[FA], Max (maxn[t], SECOND_MAX[FA] + L[T][FA]), so that the maximum distance is recorded at the same time also to record in the second large distance.
#include <vector> #include <cstring> #include <iostream> #include <algorithm>using namespace    std;const int INF = 999999999;struct Node {int v;    int C;        Node () {} node (int V, int C) {v = v;    c = C; }};int n;std::vector<node > V[10010];int fmax[10010];//first maxnint smax[10010];//second maxnint visf[10010];//    Mark the maximum distance from the computer designator int viss[10010];//mark the secondary large distance from the computer designator void init (int n) {for (int i=0; i<=n; i++) {v[i].clear ();    } for (int i=0; i<=n; i++) {fmax[i] = smax[i] =-inf;    }}void dfs0 (int t, int fa) {fmax[t] = 0;    Smax[t] = 0;        for (int i=0; i<v[t].size (); i++) {node& kid = v[t][i];        if (kid.v = = FA) continue;//cout << t << "" << kid.v <<endl;        Dfs0 (KID.V, T);            if (Fmax[t] < FMAX[KID.V] + kid.c) {smax[t] = fmax[t];            FMAX[T] = fmax[kid.v] + kid.c;            VISS[T] = visf[t];        Visf[t] = KID.V; } ElSe if (smax[t] < FMAX[KID.V] + kid.c) {smax[t] = fmax[kid.v] + kid.c;        Viss[t] = KID.V; }}}} void dfs1 (int t, int fa, int l) {if (t! = 1) {if (visf[fa] = = t) {if (Fmax[t] < SMAX[FA                ] + L) {smax[t] = fmax[t];                VISS[T] = visf[t];                FMAX[T] = Smax[fa] + L;            Visf[t] = FA;                } else if (Smax[t] < SMAX[FA] + L) {smax[t] = Smax[fa] + L;             Viss[t] = FA;                }} else {if (Fmax[t] < FMAX[FA] + L) {smax[t] = fmax[t];                VISS[T] = visf[t];                FMAX[T] = Fmax[fa] + L;            Visf[t] = FA;                } else if (Smax[t] < FMAX[FA] + L) {smax[t] = Fmax[fa] + L;             Viss[t] = FA;        }}} for (int i=0; i<v[t].size (); i++) {if (v[t][i].v = = FA) continue; DFS1 (V[T][I].V, T, v[T][I].C);        }}int Main () {while (CIN >> N) {init (n);        int A, B;            for (int i=2; i<=n; i++) {cin >> a >> b;            V[i].push_back (Node (b));        V[a].push_back (Node (i,b));        } dfs0 (1,-1);        DFS1 (1,-1, 0);        for (int i=1; i<=n; i++) {cout << fmax[i] <<endl; }} return 0;}



HDU 2196 Computer Tree DP

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.