树形DP-HDU-2196-Computer

Computer

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

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


题意:
一共有n台电脑,依次将第i台用长为L的线缆链接到已有的第j台上(j < i),求每台电脑距离最远的电脑的线缆长度是多少。


题解:
其实题目就是求树中所有节点到叶子节点的最长距离。
因为数据范围到了1E4,而且是多组,并且电脑之间的链接结构很明了是一棵树,且数据之间是有关联的,那么采用树形DP来做。
好吧上面的分析都是瞎扯,我就是搜了道树形DP来做,不服你打我呀。
首先,这个题用一次单向DFS肯定是解决不了了,因为如果随意取一个根节点,在这里以1为root为例,那么若求一次子树下最长长度,是不能肯定得到最大解的,因为还有先到父节点再走另一条路的路线可以用来取最值。
那么在定了根的情况下,要考虑的就是两点,一是子树下最长距离,二是向上一步到父节点后,非第一条路径的路径的最长距离。
那么就考虑双向DFS,然而在求来自于当前点的父节点的最大距离时,为了防止走相同的路径(即当前点子树下最长距离),我们需要保留一个父节点的子树下的次长距离,在父节点的子树下最长距离也是此节点子树下最长距离时,需要用父节点的子树下次长距离,所以DP数组为dp[maxn][3]。
其中,dp[i][0]表示从子树下得到的第i个节点到叶子节点的最大距离;
dp[i][1]表示从子树下得到的第i个节点到叶子节点的次大距离;
dp[i][2]表示从父节点得到的第i个节点到叶子节点的最大距离。
那么对于第i个节点,ans[i]=max(dp[i][0],dp[i][2])。
在第一次DFS时,可以很容易的在回溯时DP出dp[i][0/1]。
在第二次DFS时,是从根开始,向下DP(因为子节点要用到父节点的dp[][2]),对于当前点i,其父节点为fa,fa到i的距离为cost,那么dp[i][2]=max(dp[fa][2],dp[fa][0]==dp[i][0]+cost?dp[fa][1]:dp[fa][0])+cost。


//
//  main.cpp
//  HDU-2196-Computer
//
//  Created by 袁子涵 on 16/9/16.
//  Copyright © 2016年 袁子涵. All rights reserved.
//

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <string>
#include <algorithm>
#include <set>
#include <vector>
#include <queue>
#include <utility>
#include <map>

using namespace std;
const int maxn=10005;
vector<pair<int,int> >edge[maxn];
int n;
int dp[maxn][3];
void dfs1(int now,int from)
{
    int max1=0,max2=0;
    for(int i=0;i<edge[now].size();i++)
    {
        int to=edge[now][i].first,cost=edge[now][i].second;
        if(to==from)    continue;
        dfs1(to,now);
        int tmp=dp[to][0]+cost;
        if(tmp>=max1)
        {
            max2=max1;
            max1=tmp;
        }
        else if(tmp>max2)
            max2=tmp;
    }
    dp[now][0]=max1;
    dp[now][1]=max2;
}
void dfs2(int now,int from)
{
    for(int i=0;i<edge[now].size();i++)
    {
        int to=edge[now][i].first,cost=edge[now][i].second;
        if(to==from)    continue;
        dp[to][2]=max(dp[now][2],dp[to][0]+cost==dp[now][0]?dp[now][1]:dp[now][0])+cost;
        dfs2(to,now);
    }
}
int main(int argc, const char * argv[]) {
    int a,b;
    while(scanf("%d",&n)!=EOF)
    {
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=n;i++)
            edge[i].clear();
        for(int i=2;i<=n;i++)
        {
            scanf("%d%d",&a,&b);
            edge[a].push_back(make_pair(i,b));
            edge[i].push_back(make_pair(a,b));
        }
        dfs1(1,0);
        dfs2(1,0);
        for(int i=1;i<=n;i++)
            printf("%d\n",max(dp[i][0],dp[i][2]));
    }
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值