HDU - 2196 Computer 【树形DP】

Computer

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 35037    Accepted Submission(s): 5624


 

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

对于每个点,连接的是它的子节点和父节点,子节点的最大值可以跑一遍dfs得到,而对于父节点可以再跑一次dfs,通过比较父节点和子节点即可。故只需维护每个节点的第一大和第二大值即可。

#include "cstdio"
#include "cstring"
#include "queue"
#include "iostream"
#include "vector"
//#include "bits/stdc++.h"
using namespace std;
const int inf=0x3f3f3f3f;
struct edge
{
    long long v,w;
};
struct node
{
    long long v,w;
}b[10004][2];
long long dp[10004][2];
int pre[10004];
long long prelen[10004];
vector<edge>a[10004];
void dfs(int u)
{
    for (int i = 0; i < a[u].size(); ++i) {
        dfs(a[u][i].v);
        if(dp[ a[u][i].v ][0]+a[u][i].w>b[u][0].w){
            dp[u][0]=dp[ a[u][i].v ][0]+a[u][i].w;
            b[u][1]=b[u][0];
            b[u][0]={a[u][i].v,dp[ a[u][i].v ][0]+a[u][i].w};
        }
        else if(dp[ a[u][i].v ][0]+a[u][i].w>b[u][1].w)
        {
            b[u][1]={a[u][i].v,dp[ a[u][i].v ][0]+a[u][i].w};
        }
    }
}
void dfs2(int u)
{
    if(pre[u]==0)dp[u][1]=dp[u][0];
    else
    {
        int x=pre[u];
        if(u==b[x][0].v)
        {
            dp[u][1]=max(dp[u][0],prelen[u]+b[x][1].w);
            if(prelen[u]+b[x][1].w>b[u][0].w)
            {
                b[u][1]=b[u][0];
                b[u][0]={x,prelen[u]+b[x][1].w};
            }
            else if(prelen[u]+b[x][1].w>b[u][1].w)
            {
                b[u][1]={x,prelen[u]+b[x][1].w};
            }
        }
        else
        {
            dp[u][1]=max(dp[u][0],prelen[u]+b[x][0].w);
            if(prelen[u]+b[x][0].w>b[u][0].w)
            {
                b[u][1]=b[u][0];
                b[u][0]={x,prelen[u]+b[x][0].w};
            }
            else if(prelen[u]+b[x][0].w>b[u][1].w)
            {
                b[u][1]={x,prelen[u]+b[x][0].w};
            }
        }
    }
    for (int i = 0; i < a[u].size(); ++i) {
        dfs2(a[u][i].v);
    }
}
int main()
{
    int n;
    while(cin>>n)
    {
        long long u,w;
        memset(dp,0, sizeof(dp));
        memset(pre,0, sizeof(pre));
        memset(b,0, sizeof(b));
        memset(prelen,0, sizeof(prelen));
        for (int i = 2; i <= n; ++i) {
            scanf("%d%d",&u,&w);
            pre[i]=u;
            a[u].push_back({i,w});
            prelen[i]=w;
        }
        dfs(1);
        dfs2(1);
        for (int i = 1; i <= n; ++i) {
            printf("%lld\n",dp[i][1]);
        }
        for (int i = 0; i <= n; ++i) {
            a[i].clear();
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_42671946/article/details/86061755