当前位置 : 主页 > 手机开发 > harmonyos >

hdu 2196(树形dp)

来源:互联网 收集:自由互联 发布时间:2023-08-28
Computer Time Limit: 1000/1000 MS (Java/Others)Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5508Accepted Submission(s): 2748 Problem Description A school bought the first computer some time ago(so this computer's id is 1).


Computer



Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)


Total Submission(s): 5508    Accepted Submission(s): 2748




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. 



hdu 2196(树形dp)_i++



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扫一遍所以点到叶子的距离,每个点维护两个值,一个是到叶子节点最长和次长的距离。dp[i][0]为i为根节点的子树到叶子节点的最长 dp[i][1]为次长。dp[i][2]为i节点从父亲路径过来的最长距离


dp[i][2]要考虑i节点是否在i节点父亲的最长路径上具体看代码。。。


#include <bits/stdc++.h>
using namespace std;
const int N=1e4+5;

vector<pair<int,int> >G[N];

int dp[N][3];

void dfs(int v)
{
    int s1=0,s2=0;
    for(int i=0;i<G[v].size();i++)
    {
        int e=G[v][i].first;
        int len=G[v][i].second;
        dfs(e);
        if(s1<dp[e][0]+len)
        {
            s2=s1;
            s1=dp[e][0]+len;
        }
        else if(s2<dp[e][0]+len)
            s2=dp[e][0]+len;

    }
    dp[v][0]=s1;
    dp[v][1]=s2;
}

void dfs1(int v)
{
    for(int i=0;i<G[v].size();i++)
    {
        int e=G[v][i].first;
        int len=G[v][i].second;
        dp[e][2]=max(dp[v][2],dp[e][0]+len==dp[v][0]?dp[v][1]:dp[v][0])+len; //
        dfs1(e);
    }
}

int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        for(int i=1;i<=n;i++)
            G[i].clear();
        memset(dp,0,sizeof(dp));
        for(int i=2;i<=n;i++)
        {
            int u,len;
            scanf("%d%d",&u,&len);
            G[u].push_back({i,len});
        }
        dfs(1);
    //    for(int i=1;i<=n;i++)
     //       printf("%d ",dp[i][0]);
        dfs1(1);
        for(int i=1;i<=n;i++)
            printf("%d\n",max(dp[i][2],dp[i][0]));
    }
    return 0;
}



上一篇:hdu 3586(树形dp+二分)
下一篇:没有了
网友评论