Computer HDU - 2196

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

题意:求树上每个点能达到的最远距离

思路:树形dp

dp[i][sta]表示第i个节点(sta==0,表示节点i的子树最远距离,sta==1,表示节点i的子树的次远距离,sta==3表示走父节点的最远距离)

需要两遍DFS,因为dfs时保存的状态不同,所以需要注意状态转移的位置。

代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <map>
#include <vector>
#include <set>
#include <string>
#include <math.h>
#include <stack>
typedef long long ll;
using namespace std;
const int maxn=1e5+10;
const int INF=1e9;
const int mod=1e9+7;
struct Edge
{
    int u,v,w,next;
}edge[maxn];
int num,n;
int head[maxn];
int dp[maxn][3];
bool vis[maxn];
int idx[maxn];
void addEdge(int u,int v,int w)
{
    edge[num].u=u;
    edge[num].v=v;
    edge[num].w=w;
    edge[num].next=head[u];
    head[u]=num++;
}
void init()
{
    memset(head,-1,sizeof(head));
    memset(dp,0,sizeof(dp));
    num=0;
}
void dfs1(int s,int pre)
{
    ll mx0=0,mx1=0;
    for(int i=head[s];i!=-1;i=edge[i].next)
    {
        int v=edge[i].v;
        int w=edge[i].w;
        if(v==pre) continue;
        dfs1(v,s);
        if(mx0<=dp[v][0]+w)
        {
            mx1=mx0;//找到更大的,则直接把以前的给第二大
            mx0=dp[v][0]+w;
            idx[s]=v;//记录最大值走过的的直接子节点
        }
        else if(mx1<dp[v][0]+w)
        {
            mx1=dp[v][0]+w;
        }
        else if(mx1<dp[v][1]+w)
        {
            mx1=dp[v][1]+w;
        }
    }
    dp[s][0]=mx0,dp[s][1]=mx1;
}
void dfs2(int s,int pre)
{
    for(int i=head[s];i!=-1;i=edge[i].next)
    {
        int v=edge[i].v,w=edge[i].w;
        if(pre==v) continue;
        if(idx[s]==v)//如果父节点最大的值是经过它的,那么我们选择走第二大的
        {
            dp[v][2]=max(dp[s][1]+w,dp[s][2]+w);
        } 
        else//否则直接选择最大的
        {
            dp[v][2]=max(dp[s][0]+w,dp[s][2]+w);
        }
        dfs2(v,s);
    }
}
int main(int argc, char const *argv[])
{
    while(scanf("%d",&n)!=EOF)
    {
        init();
        for(int i=2;i<=n;i++)
        {
            int x,w;
            scanf("%d%d",&x,&w);
            addEdge(i,x,w);
            addEdge(x,i,w);
        }
        dfs1(1,1);
        dfs2(1,1);
        for(int i=1;i<=n;i++)
        {
            printf("%d\n",max(dp[i][0],dp[i][2]));
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40774175/article/details/81350145
今日推荐