HDU 2196 Computer【树形DP】

 

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
分析:  两次DFS 第一次 找出每个节点的最远和次远距离  自下往上
               第二次 找出每个节点不包含该节点下方节点的最远距离。
View Code
#include<stdio.h>
#include<string.h>
int max(int a,int b)
{
    return a>b?a:b;
}
struct node
{
    int to,next,w;
}q[1000000];
int tot;
int head[10005];
int f[10005];   // 当前节点往下的最远距离
int s[10005];   // 当前节点往下的次远距离
int d[10005];   // 当前节点不包括该节点下方的最远距离
int l[10005];   // 当前节点最远路径上的直接儿子
void add(int v,int u,int wi)
{
    q[tot].to=u;
    q[tot].next=head[v];
    q[tot].w=wi;
    head[v]=tot++;
}
void dfs(int r)
{
    int i;
    if(f[r])return;
    int son,flag=1;
    int tmp=-1,xi=-1;
    for(i=head[r];i;i=q[i].next)
    {
        flag=0;
        son=q[i].to;
        dfs(son);
        if(f[son]+q[i].w>tmp)
        {
            tmp=f[son]+q[i].w;
            xi=son;
        }
    }
    if(flag) 
    {
        f[r]=0;
        s[r]=0;
        return;
    }
    f[r]=tmp;
    l[r]=xi;
    tmp=-1;
    int x2=-1;
    for(i=head[r];i;i=q[i].next)
    {
        son=q[i].to;
        if(son!=xi&&f[son]+q[i].w>tmp)
        {
            tmp=f[son]+q[i].w;
            x2=son;
        }
    }
    if(x2!=-1)
        s[r]=tmp;
}
void dis(int r)
{
    int i,son;
    for(i=head[r];i;i=q[i].next)
    {
        son=q[i].to;
        if(son==l[r])
            d[son]=max(d[r],s[r])+q[i].w;
        else
            d[son]=max(d[r],f[r])+q[i].w;
        dis(son);
    }    
}
int main()
{
    int n,i,a,b;
    while(scanf("%d",&n)!=EOF)
    {
        tot=1;
        memset(head,0,sizeof(head));
        memset(f,0,sizeof(f));
        memset(s,0,sizeof(s));
        memset(d,0,sizeof(d));
        memset(l,-1,sizeof(l));
        for(i=2;i<=n;i++)  
        {
            scanf("%d%d",&a,&b);
            add(a,i,b);
        }
        dfs(1);
        d[1]=0;
        dis(1);
        for(i=1;i<=n;i++)
        {
            int res=max(s[i],d[i]);
            res=max(res,f[i]);
            printf("%d\n",res);
        }
    }
    return 0;
}
posted @ 2012-04-18 22:14  'wind  阅读(353)  评论(0编辑  收藏  举报