hdu2196 Computer【树形DP】【换根法】

计算树中最远距离
本文介绍了一种算法,用于解决给定一棵有n个节点的树,每条边都有权值,求每个节点到其它所有节点的最远距离的问题。通过两次深度优先搜索,一次寻找子树中的最远距离,另一次考虑不在子树中的最远距离。

Computer

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


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   |   We have carefully selected several similar problems for you:   1561  1011  3456  1520  2242 

 

题意:

一棵有n个节点的树,有权边。问每个节点到其他节点的最远距离分别是多少。

思路:

如果固定一个节点,那么dfs找最远的距离就可以了。用dp[i]表示以i为根的子树中,i可到的最远距离。

对于任意一个节点i,他到达的最远节点有可能在子树中,也有可能不在子树中。所以我们分别找到子树中的最远距离和不在子树中的最远距离。

dfs1就是用dp存储了在子树中的最远距离。

dfs2就是把当前节点换到了根的位置,用f存储不在子树中的最远距离。那么他到他子树节点的距离是变短了的就不用管了。

把i换成根,把i的父亲father换下去,如果i在i的父亲这棵子树的最长路径上。father换下去之后,就要考虑原本以father为根的子树的第二长路径。

如果i不在father的最长路径上,那就是在f和dp中找大的加上当前路径长度。

 

 1 //#include <bits/stdc++.h>
 2 #include<iostream>
 3 #include<cmath>
 4 #include<algorithm>
 5 #include<stdio.h>
 6 #include<cstring>
 7 #include<vector>
 8 #include<map>
 9 #include<set>
10 
11 #define inf 0x3f3f3f3f
12 using namespace std;
13 typedef long long LL;
14 
15 const int maxn = 10005;
16 int head[maxn], dp[maxn], f[maxn], second[maxn], longest[maxn];
17 struct node{
18     int v;
19     int nxt;
20     int weight;
21 }edge[maxn * 2];
22 int n, cnt;
23 
24 void addedge(int u, int v, int w)
25 {
26     edge[cnt].v = v;
27     edge[cnt].weight = w;
28     edge[cnt].nxt = head[u];
29     head[u] = cnt++;
30     edge[cnt].v = u;
31     edge[cnt].weight = w;
32     edge[cnt].nxt = head[v];
33     head[v] = cnt++;
34 }
35 
36 void dfs1(int rt, int fa)
37 {
38     for(int i = head[rt]; i != -1; i = edge[i].nxt){
39         int son = edge[i].v;
40         if(son == fa)continue;
41         dfs1(son, rt);
42         //dp[rt] = max(dp[rt], dp[son] + edge[i].weight);
43         if(dp[son] + edge[i].weight > dp[rt]){
44             longest[rt] = son;
45             second[rt] = max(second[rt], dp[rt]);
46             dp[rt] = dp[son] + edge[i].weight;
47         }
48         else if(second[rt] < dp[son] + edge[i].weight){
49             second[rt] = dp[son] + edge[i].weight;
50         }
51     }
52 }
53 
54 void dfs2(int rt, int fa)
55 {
56     for(int i = head[rt]; i != -1; i = edge[i].nxt){
57         int son = edge[i].v;
58         if(son == fa)continue;
59         if(longest[rt] == son)f[son] = max(f[rt], second[rt]) + edge[i].weight;
60         else f[son] = max(f[rt], dp[rt]) + edge[i].weight;
61         dfs2(son, rt);
62     }
63 }
64 
65 int main(){
66     while(scanf("%d", &n) != EOF){
67         cnt = 0;
68         memset(head, -1, sizeof(head));
69         for(int i = 2; i <= n; i++){
70             int u, w;
71             scanf("%d%d", &u, &w);
72             addedge(u, i, w);
73         }
74         memset(dp, 0, sizeof(dp));
75         memset(second, 0, sizeof(second));
76         memset(longest, 0, sizeof(longest));
77         memset(f, 0, sizeof(f));
78         dfs1(1, 0);
79 
80         dfs2(1, 0);
81         //f[1] = dp[1];
82         for(int i = 1; i <= n; i++){
83             printf("%d\n", max(dp[i], f[i]));
84         }
85     }
86     return 0;
87 }

 

转载于:https://www.cnblogs.com/wyboooo/p/9782945.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值