ComputerTime
limit:1000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 3651 Accepted Submission (s): 1852
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
#include <iostream> #include <cstring> #include <cstdio> #include <queue> #include < Algorithm> #include <vector>using namespace std;typedef long long ll; #define M 10005vector<int> Adj[m],wi [M];ll f[m][2];int vis[m];ll dfs1 (int u) {vis[u]=1; f[u][0]=0; for (int i=0;i<adj[u].size (); i++) {int v=adj[u][i]; int w=wi[u][i]; if (Vis[v]) continue; F[u][0]=max (F[U][0],DFS1 (v) +w); } return f[u][0];} void dfs2 (int u) {vis[u]=1; int m1=0,m2=0,v1,v2; for (int i=0;i<adj[u].size (); i++) {int v=adj[u][i]; int w=wi[u][i]; if (Vis[v]) continue; int tmp=f[v][0]+w; if (tmp>m1) {m2=m1,v2=v1; M1=tmp,v1=v; } else if (m1==tmp | | tmp>m2) {m2=tmp,v2=v; }} if (u!=1) {int tmp=f[u][1]; int v=-1; if (tmp>m1) {m2=m1,v2=v1; M1=tmp,v1=v; } else if (m1==tmp | | tmp>m2) { M2=tmp,v2=v; }} for (int i=0;i<adj[u].size (); i++) {int v=adj[u][i]; int w=wi[u][i]; if (Vis[v]) continue; if (V==V1) f[v][1]=m2+w; else f[v][1]=m1+w; DFS2 (v); }}int Main () {int n; while (Cin>>n && N) {for (int i=0;i<=n;i++) adj[i].clear (), wi[i].clear (); for (int u=2;u<=n;u++) {int v,w; cin>>v>>w; Adj[u].push_back (v); Wi[u].push_back (w); Adj[v].push_back (U); Wi[v].push_back (w); } memset (f,0,sizeof f); memset (vis,0,sizeof Vis); DFS1 (1); memset (vis,0,sizeof Vis); DFS2 (1); for (int j=1;j<=n;j++) Cout<<max (f[j][0],f[j][1]) <<endl; }}
HDU 2196 Computer DP