Computer
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1232 Accepted Submission(s): 597
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
Recommend
lcy
AC代码:
/*
2017/3/1
题目问每个节点到达叶子结点的最大长度?
思路:
某一节点能到达的最远的叶子结点可能是以他为子树的叶子节点,也可能是经过他的父节点后能达到的最远的叶子节点;
所以分两次进行: 将无根树转换成有根树(以节点1为根),直接在搜索时注意不返回父节点即可
注意:由于我们要维护两个值,即最远和次远,那么每次比较时先和次远距离比较,再和最远距离比较
1):
深度搜索一遍找出以每个节点i为根节点,在该子树上到叶子节点的最远距离maxans[i],和次远距离smaxans[i]; 最远距离和次远距离来自不同的子节点
并记录最远距离经过的第一个孩子节点的编号maxid[i],和次远距离经过的第一个孩子节点的编号smaxid[i];
2):
可能是经过父节点后的路径,所以再用父节点的最远距离或次远距离更新已经得到的结果
再从根节点开始深度搜索一遍,设父节点为u,他的孩子节点为v,
那么如果maxid[u] == v (父节点的最远距离经过了该孩子节点,所以我们只能用次远距离来更新)
如果edges[i].len + maxans[u] > smaxans[v] ,则更新次远信息
如果smaxans[v] > maxans[v] 则交换次远和最远
*/
#include <iostream>
#include <cstdio>
#include <cstring>z
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
const int maxn = 10010;
struct edge
{
int v,len,next;
}edges[maxn*2];
int head[maxn];
int e,n;
int maxans[maxn],smaxans[maxn];
int maxid[maxn],smaxid[maxn];
void addedge(int u,int v,int len)
{
edges[e].v = v;
edges[e].len = len;
edges[e].next = head[u];
head[u] = e++;
edges[e].v = u;
edges[e].len = len;
edges[e].next = head[v];
head[v] = e++;
}
void dfs1(int u,int p)
{
maxans[u] = smaxans[u] = 0;
for(int i=head[u];i!=-1;i=edges[i].next)
{
int v = edges[i].v;
if(v == p) continue;//不能返回父节点
dfs1(v,u);
if(maxans[v] + edges[i].len > smaxans[u])
{
smaxans[u] = maxans[v] + edges[i].len;
smaxid[u] = v;
if(smaxans[u] > maxans[u])
{
swap(maxans[u],smaxans[u]);
swap(maxid[u],smaxid[u]);
}
}
}
}
void dfs2(int u,int p)
{
for(int i=head[u];i!=-1;i=edges[i].next)
{
int v = edges[i].v;
if(v == p) continue;
if(v == maxid[u])
{
if(edges[i].len + smaxans[u] > smaxans[v])//如果大于次短边
{
smaxans[v] = edges[i].len + smaxans[u];
smaxid[v] = u;
if(smaxans[v] > maxans[v])//如果大于最大边
{
swap(smaxans[v],maxans[v]);
swap(smaxid[v],maxid[v]);
}
}
}
else
{
if(edges[i].len + maxans[u] > smaxans[v])//如果大于次短边
{
smaxans[v] = edges[i].len + maxans[u];
smaxid[v] = u;
if(smaxans[v] > maxans[v])//如果大于最大边
{
swap(smaxans[v],maxans[v]);
swap(smaxid[v],maxid[v]);
}
}
}
dfs2(v,u);
}
}
int main()
{
int v,len;
while(scanf("%d",&n)!=EOF)
{
e =0;
memset(head,-1,sizeof(head));
for(int i=2;i<=n;i++)
{
scanf("%d%d",&v,&len);
addedge(i,v,len);
}
dfs1(1,-1);
dfs2(1,-1);
for(int i=1;i<=n;i++)
{
printf("%d\n",maxans[i]);
}
}
return 0;
}