题解报告:hdu 2196 Computer(树形dp)

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

解题思路:

AC代码(31ms):

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int maxn=10005;
 4 struct node{int to,next,len;}edge[maxn<<1];
 5 int n,x,y,cnt,head[maxn],dp[maxn][3],lgst[maxn];
 6 void add_edge(int u,int v,int w){
 7     edge[cnt].to=v;
 8     edge[cnt].len=w;
 9     edge[cnt].next=head[u];
10     head[u]=cnt++;
11 }
12 int dfs1(int u,int fa){
13     int Dmax=0,Dsec=0;
14     for(int i=head[u];~i;i=edge[i].next){
15         int v=edge[i].to;
16         if(v^fa){
17             int nowd=dfs1(v,u)+edge[i].len;
18             if(nowd>Dmax)lgst[u]=v,Dsec=Dmax,Dmax=nowd;
19             else if(nowd>Dsec)Dsec=nowd;
20         }
21     }
22     dp[u][0]=Dmax,dp[u][1]=Dsec;
23     return Dmax;
24 }
25 void dfs2(int u,int fa){
26     for(int i=head[u];~i;i=edge[i].next){
27         int v=edge[i].to;
28         if(v^fa){
29             if(v==lgst[u])dp[v][2]=max(dp[u][2],dp[u][1])+edge[i].len;
30             else dp[v][2]=max(dp[u][2],dp[u][0])+edge[i].len;
31             dfs2(v,u);
32         }
33     }
34 }
35 int main(){
36     while(~scanf("%d",&n)){
37         cnt=0;memset(head,-1,sizeof(head));
38         memset(dp,0,sizeof(dp));
39         memset(lgst,0,sizeof(lgst));
40         for(int i=2;i<=n;++i){
41             scanf("%d%d",&x,&y);
42             add_edge(i,x,y);
43             add_edge(x,i,y);
44         }
45         dfs1(1,-1);
46         dfs2(1,-1);
47         for(int i=1;i<=n;++i)
48             printf("%d\n",max(dp[i][0],dp[i][2]));
49     }
50     return 0;
51 }

原文地址:https://www.cnblogs.com/acgoto/p/9607627.html

时间: 2024-08-03 19:22:26

题解报告:hdu 2196 Computer(树形dp)的相关文章

HDU 2196 Computer 树形DP经典题

链接:http://acm.hdu.edu.cn/showproblem.php? pid=2196 题意:每一个电脑都用线连接到了还有一台电脑,连接用的线有一定的长度,最后把全部电脑连成了一棵树,问每台电脑和其它电脑的最远距离是多少. 思路:这是一道树形DP的经典题目.须要两次DFS,第一次DFS找到树上全部的节点在不同子树中的最远距离和次远的距离(在递归中进行动态规划就可以),第二次DFS从根向下更新出终于答案.对于每次更新到的节点u,他的最远距离可能是来自u的子树,或者是u的父亲节点的最远

HDU 2196 Computer 树形DP 经典题

给出一棵树,边有权值,求出离每一个节点最远的点的距离 树形DP,经典题 本来这道题是无根树,可以随意选择root, 但是根据输入数据的方式,选择root=1明显可以方便很多. 我们先把边权转化为点权,放在数组cost中 令tree(i)表示以节点i为根的子树 对于节点i,离该节点最远的点要不就是在tree(i)中,要不就是在father(i)上面 令: dp[i][1] : 在子树tree(i)中,离i最远的距离 dp[i][2] : 在子树tree(i)中,离i第二远的距离 (递推的时候需要)

HDU 2169 Computer[树形dp]

Computer 时限:1000ms 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 s

HDU 2196(树形dp

题目:求出一棵树上的所有点到其他点的最长距离. 思路:取一个根节点进行dfs,先求出每个节点到子节点的最长路和次长路(也就是与最长路不同的最长的路,有可能与最长路长度相等),并记录最长路和次长路通过的相邻节点的标号.然后进行第二次dfs,考虑最长路是通过父节点的情况,如果该节点v在父节点的最长路上,那么需要取次长路,否则就取最长路. 第二次dfs的时候最长路的通过节点还是要更新,因为若某个父节点的最长路是朝祖先走的,那么其子节点的最长路一定朝祖先走,且与v是否在父节点向下的最长路上无关. #in

HDU 2196 Computer 经典树形DP

一开始看错题了,后来发现原来是在一颗带权的树上面求出距离每一个点的最长距离,做两次dfs就好,具体的看注释? #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #incl

hdu 1011(树形dp)

Mark.看着吴神博客写的,还未完全懂. 1 #include <stdio.h> 2 #include <string.h> 3 #include <iostream> 4 #include <algorithm> 5 #include <vector> 6 #include <queue> 7 #include <set> 8 #include <map> 9 #include <string>

HDU 2196Computer(树形DP)

给你一颗边带权值的树,求树上的每一点距离其最远的一个点的距离 比较典型的题了,主要方法是进行两次DFS,第一次DFS求出每一个点距离它的子树的最远距离和次远距离,然后第二次DFS从父节点传过来另一侧的树上的距离它的最远距离进行一次比较便可得出任意点的最远距离了 之所以需要记录最远和次远是为了辨别父节点的最远距离是否是根据自己得来,如果是的话应该选择父节点的次远距离,保证结果的准确性 1 //#pragma comment(linker,"/STACK:102400000,102400000&qu

树形DP [HDU 2196] Computer

Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3526    Accepted Submission(s): 1788 Problem Description A school bought the first computer some time ago(so this computer's id is 1). D

HDU 2196 树状dp 求树中节点之间的最长距离

Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3749    Accepted Submission(s): 1892 Problem Description A school bought the first computer some time ago(so this computer's id is 1). Du

HDU-2196 Computer (树形DP)

最近在看树形DP,这题应该是树形DP的经典题了,写完以后还是有点感觉的.之后看了discuss可以用树分治来做,以后再试一试. 题目大意 找到带权树上离每个点的最远点.︿( ̄︶ ̄)︿ 题解: 对于每一个点的最远点,就是以这个点为根到所有叶子节点的最长距离.但是如果确定根的话,除了根节点外,只能找到每个节点(度数-1)个子树的最大值,剩下一个子树是该节点当前的父亲节点. 所以当前节点的最远点在当前节点子树的所有叶子节点以及父亲节点的最远点上(当父亲节点的最远点不在当前节点的子树上时), 如果父亲节