標籤:hdu treedp acm
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 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 Input51 12 13 11 1 Sample Output32344 有一個電腦群組成的無向樹狀網路,要求求出每台電腦到其他電腦的最大距離。原先有1號電腦,在此之後沒加如一台電腦都與原有的一台電腦串連,並有距離c。題中資料較大,所以不能對每個節點進行遞迴求深度。仔細想想,在以1號電腦為根節點的情況下,每個節點的最大距離來自其父節點或其子節點。來自子節點的情況比較簡單,我們只要對其求深度就行maxn[t] = max(maxn[ti] + l[ti][fa])。而父節點就要分情況,對於求的t節點,其父節點為fa節點,若fa節點的最大距離不是經過t節點來的,那麼t節點的最大距離就是max(maxn[t], maxn[fa] + l[t][fa]);如果是經過t節點來的,那依然可以從fa節點過來,不過路徑不是最大距離那條而是次大距離那條——second_maxn[fa], max(maxn[t], second_max[fa] + l[t][fa]);因此,在記錄最大距離的同時也要記入次大距離。
#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];//標記最大距離來自的電腦標號int viss[10010];//標記次大距離來自的電腦標號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(a,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 樹狀dp