Computer
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
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.
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
题意:给出一棵树,求结点到叶子的最大权值
对于每个结点,如果每一次都暴力寻找最远距离,那么复杂度达到O(n^2),不可取
但是由于是一棵树,那么对于每个结点来说,最远距离要么是在子树中,要么是从父节点过来的最远距离
那么就有了状态转移的方式,我们进行两次dfs,第一次统计每个结点的子树最远距离和次远距离,
第二次从根节点出发更新每个结点到叶子结点的最远距离和次远距离,那么这样一来就统计了每个结点的最大距离
时间复杂度O(n)
#include<cstring>
#include<string>
#include<iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#include<map>
#include<cstdlib>
#include<cmath>
#include<vector>
//#pragma comment(linker, "/STACK:1024000000,1024000000");
using namespace std;
#define INF 0x3f3f3f3f
#define maxn 20004
int dp[maxn][2],id[maxn];
int fir[maxn],nex[maxn],u[maxn],v[maxn],w[maxn];
int e_max;
void init()
{
memset(dp,0,sizeof dp);
memset(fir,-1,sizeof fir);
for(int i=0;i<maxn;i++) id[i]=i;
e_max=0;
}
void add_edge(int s,int t,int c)
{
int e=e_max++;
u[e]=s;
v[e]=t;
w[e]=c;
nex[e]=fir[s];
fir[s]=e;
}
void dfs1(int k)
{
for(int i=fir[k]; ~i; i=nex[i])
{
dfs1(v[i]);
int e=v[i];
if(dp[e][1]+w[i]>dp[k][1])
{
dp[k][0]=dp[k][1];
dp[k][1]=dp[e][1]+w[i];
id[k]=e;
}
else if(dp[e][1]+w[i]>dp[k][0])
{
dp[k][0]=dp[e][1]+w[i];
}
}
}
void dfs2(int k)
{
for(int i=fir[k];~i;i=nex[i])
{
int e=v[i];
if(id[k]==e)
{
if(dp[k][0]+w[i]>dp[e][1])
{
dp[e][0]=dp[e][1];
dp[e][1]=dp[k][0]+w[i];
id[e]=k;
}
else if(dp[k][0]+w[i]>dp[e][0])
{
dp[e][0]=dp[k][0]+w[i];
}
}
else
{
if(dp[k][1]+w[i]>dp[e][1])
{
dp[e][0]=dp[e][1];
dp[e][1]=dp[k][1]+w[i];
id[e]=k;
}
else if(dp[k][1]+w[i]>dp[e][0])
{
dp[e][0]=dp[k][1]+w[i];
}
}
dfs2(e);
}
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
init();
for(int i=2; i<=n; i++)
{
int a,b;
scanf("%d%d",&a,&b);
add_edge(a,i,b);
}
dfs1(1);
dfs2(1);
for(int i=1;i<=n;i++)
{
printf("%d\n",dp[i][1]);
}
}
return 0;
}