**Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4646 Accepted Submission(s): 2345
**
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.
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
Author
scnu
据说是经典的树形dp,我看了很久题解才看懂为什么要记录以该节点为根的其子树范围内的最大距离和次大路,因为对于一个节点来说,可能得到的距离最大的值的路径来自他的子树,或者从他的父节点过来,所以用两次dfs。第一次DFS求出所有节点在他的子树范围内到叶子节点距离的最大距离和次大路,并且记录最大距离和次大路的叶子节点的编号,第二次DFS更新从父节点过来的情况就可以了。因为如果只存最大值的话,判断一个点的从父节点过来的最大值,那么如果他的父节点存的最大值正好是从该点过来的,那么就失去了从父节点过来的状态,这时要用到这个父节点的次大值。
一开始总疑惑为什么要求次短路,原来是父亲节点的转移处会用到,对自身节点没有用。
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
#define inf 99999999
#define maxn 10060
struct node{
int len,to,next;
}e[2*maxn];
int first[maxn],vis[maxn],dist1[maxn],dist2[maxn],ans[maxn];//dist1[i]表示最大路,dist2[i]表示次大路,dist1id[i]表示得到最大路的叶子节点编号,dist2id[i]表示得到次大路的叶子节点编号
int dist1id[maxn],dist2id[maxn];
void dfs(int u)
{
int i,j,t1,t2,flag;
t1=t2=0;
vis[u]=1;
flag=0;
for(i=first[u];i!=-1;i=e[i].next){
int v=e[i].to;
if(vis[v])continue;
flag=1;
dfs(v);
int t=e[i].len+dist1[v];
if(t>=dist1[u]){
dist2[u]=dist1[u];
dist1[u]=t;
dist2id[u]=dist1id[u];
dist1id[u]=dist1id[v];
}
else if(t>dist2[u]){
dist2[u]=t;
dist2id[u]=dist1id[v];
}
}
if(flag==0){
dist1[u]=dist2[u]=0;
dist1id[u]=dist2id[u]=u;return;
}
}
void dfs1(int u)
{
int i,j,t,t1,t2;
t1=t2=0;
vis[u]=1;
for(i=first[u];i!=-1;i=e[i].next){
int v=e[i].to;
if(vis[v])continue;
if(dist1id[u]==dist1id[v]){ //这里先看一下父亲节点所求出的最大路的叶子节点编号是不是和当前节点相同
int t=e[i].len+dist2[u];
if(t>=dist1[v]){
dist2[v]=dist1[v];
dist2id[v]=dist1id[v];
dist1[v]=t;
dist1id[v]=dist2id[u];
}
else if(t>dist2[v]){
dist2[v]=t;
dist2id[v]=dist2id[u];
} //要随时更新dist1[i],这里dist1[i]已经不仅是子树范围了,而是全部范围
}
else{
int t=e[i].len+dist1[u];
if(t>=dist1[v]){
dist2[v]=dist1[v];
dist2id[v]=dist1id[v];
dist1[v]=t;
dist1id[v]=dist1id[u];
}
else if(t>dist2[v]){
dist2[v]=t;
dist2id[v]=dist1id[u];
}
}
dfs1(v);
}
}
int main()
{
int n,m,i,j,c,d;
while(scanf("%d",&n)!=EOF)
{
memset(first,-1,sizeof(first));
int tot=0;
for(i=2;i<=n;i++){
scanf("%d%d",&c,&d);
int u,v;
u=i;v=c;
tot++;
e[tot].next=first[u];e[tot].to=v;e[tot].len=d;
first[u]=tot;
tot++;
e[tot].next=first[v];e[tot].to=u;e[tot].len=d;
first[v]=tot;
}
memset(vis,0,sizeof(vis));
memset(dist1,0,sizeof(dist1));
memset(dist2,0,sizeof(dist2));
memset(dist1id,0,sizeof(dist1id));
memset(dist2id,0,sizeof(dist2id));
dfs(1);
memset(vis,0,sizeof(vis));
dfs1(1);
for(i=1;i<=n;i++){
printf("%d\n",dist1[i]);
}
}
return 0;
}
手机扫一扫
移动阅读更方便
你可能感兴趣的文章