Computer
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2091 Accepted Submission(s): 1060
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.
![](https://i-blog.csdnimg.cn/blog_migrate/3f5ab5c3d97280e16503ce9ef614b5c5.jpeg)
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题意:给出一棵树,求离每个节点最远的点与它的距离。思路:树形dp。设down[u]为节点u与它的子孙的最远距离,up[u]为u经过其父节点与其他点的最远距离。设id[u][0]表示u与它子孙的最远距离上u的儿子,显然有down[u]=down[id[u][0]]+w(u,id[u][0]), id[u][1]表示u与它子孙的次远距离上u的儿子。第一次dfs求down[u],每次用子节点的down值去更新父节点的down值,并记录id[u][0]和id[u][1]。第二次dfs借助第一次dfs求出的down[u]去求up[u],对于一个节点u,有两种情况:1. u在父节点的最远距离的分支上,此时up[u]=max(up[fa]+w(fa,u),down[id[fa][1]]+w(fa,id[fa][1])+w(fa,u)).2. u不在父节点的最远距离的分支上,此时up[u]=max(up[fa],down[fa])+w(fa,u).组后ans[i]=max(up[u],down[u]).AC代码:#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <algorithm> #include <queue> #include <vector> #include <cmath> #include <cstdlib> #define L(rt) (rt<<1) #define R(rt) (rt<<1|1) using namespace std; const int maxn=10005; const int INF=1000000000; struct node { int v,w,next; } edge[3*maxn]; int n,num; int up[maxn],down[maxn],dis[maxn]; int id[maxn][2],head[maxn]; void init() { memset(head,-1,sizeof(head)); num=0; } void add(int u,int v,int w) { edge[num].v=v; edge[num].w=w; edge[num].next=head[u]; head[u]=num++; } void input() { int v,w; for(int i=2;i<=n;i++) { scanf("%d%d",&v,&w); add(i,v,w); add(v,i,w); } } void dfs1(int u,int fa) { down[u]=0; id[u][0]=id[u][1]=-1; for(int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].v,w=edge[i].w; if(v==fa) continue; dfs1(v,u); dis[v]=w; if(down[v]+w>=down[u]) { down[u]=down[v]+w; id[u][1]=id[u][0]; id[u][0]=v; } else if(id[u][1]==-1||down[v]+w>down[id[u][1]]+dis[id[u][1]]) id[u][1]=v; } } void dfs2(int u,int fa) { up[u]=0; if(fa!=-1) { if(u==id[fa][0]) { up[u]=up[fa]+dis[u]; if(id[fa][1]!=-1) up[u]=max(up[u],down[id[fa][1]]+dis[id[fa][1]]+dis[u]); } else up[u]=max(up[fa],down[fa])+dis[u]; } for(int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].v; if(v==fa) continue; dfs2(v,u); } } void solve() { dfs1(1,-1); dfs2(1,-1); for(int i=1;i<=n;i++) printf("%d\n",max(up[i],down[i])); } int main() { while(~scanf("%d",&n)) { init(); input(); solve(); } return 0; }