HDU 2196【树形dp*好题】

本文介绍了一种计算树形结构中每个节点到其它所有节点最大距离的方法。通过建立有根树并采用两次深度优先搜索(DFS)策略,文章详细阐述了如何高效地找出从任一节点出发到达最远节点的距离。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

题意:给出一颗树,求树中的每个顶点到其他所有顶点的距离最大值。

思路:

建立一棵有根树,计算 i 点的最大距离:dis = max(子节点最大距离,不经过子节点这条路线的最大距离),那么计算i 点的最大距离就变成除去子节点后再找一条最长距离(它可能是i顶点的另一条分支,另一种可能是向树的上方查找最大距离),这就有三个距离值了;

预处理的时候,需要保存两段距离,这是一颗有根树,在以i为根节点的树上,保存距根节点i的最大距离和次大距离(DFS从树的底部向上跟新到i),还差向上统计最大距离。此时需要写出转移方程(DFS从树的根部向下更新到叶子):

dp[e.to][2] = max(dp[fa][2], dp[e.to][0] + e.cost == dp[fa][0] ? dp[fa][1] : dp[fa][0]) + e.cost;

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;

const int MAXN = 1e4 + 10;
int dp[MAXN][5];

struct node {
    int to;
    int cost;
};

vector<node> V[MAXN];

void init() {
	memset(dp, 0, sizeof(dp));
	for(int i = 0; i < MAXN; i++) {
		V[i].clear();
	}
}

void dfs1(int x) {
    int ans1 = 0, ans2 = 0;
    for(int i = 0; i < V[x].size(); i++) {
        node e = V[x][i];
        dfs1(e.to);
        int res = dp[e.to][0] + e.cost;
        if(res >= ans1) {
        	ans2 = ans1;
			ans1 = res;
		}
		else if(res > ans2){
			ans2 = res;
		}
    }
    dp[x][0] = ans1;
    dp[x][1] = ans2;
}

void dfs2(int fa) {
	for(int i = 0; i < V[fa].size(); i++) {
		node e = V[fa][i];
		dp[e.to][2] = max(dp[fa][2], dp[e.to][0] + e.cost == dp[fa][0] ? dp[fa][1] : dp[fa][0]) + e.cost;
		dfs2(e.to);
	}
}

int main() {
    int n;
    while(scanf("%d", &n) != EOF) {
    	init(); 
        int p1, p2;
        for(int i = 2; i <= n; i++) {
            scanf("%d %d", &p1, &p2);
            node e;
            e.to = i;
            e.cost = p2;
            V[p1].push_back(e);
        }
        dfs1(1);
        dfs2(1);
        for(int i = 1; i <= n; i++) {
        	printf("%d\n", max(dp[i][0], dp[i][2]));
		}
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值