HDU 2196 - Computer

参考来自http://blog.csdn.net/shuangde800/article/details/9732825的思路:

跟思路中不太相同的是,我们如下设置DP数组:

dp[i][0] : 表示以i为根的子树中的结点与i的最大距离

dp[i][1] : 表示以i为根的子树中的结点与u的次大距离(即上述思路中的secondDist)

dp[i][2] : 表示i往父亲节点方向走的最大距离

同样的,我们也做两次DFS,但是第一次的时候,我们将dp[i][0]和dp[i][1]一起算出来;

第二次DFS就可以直接用dp[i][0]和dp[i][1]。

代码如下:

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<vector>
 4 #include<cstring>
 5 #define MAXN 10000+5
 6 using namespace std;
 7 //dp[i][0] : 表示以i为根的子树中的结点与i的最大距离
 8 //dp[i][1] : 表示以i为根的子树中的结点与u的次大距离
 9 //dp[i][2] : 表示i往父亲节点方向走的最大距离
10 int n,dp[MAXN][3],idx[MAXN];
11 struct Edge{
12     int u,v,w;
13 };
14 vector<Edge> child[MAXN];
15 void dfs1(int now)
16 {
17     if(child[now].size()==0)
18     {
19         dp[now][0]=0;
20         dp[now][1]=0;
21         return;
22     }
23     for(int i=0;i<child[now].size();i++)
24     {
25         Edge edge=child[now][i];
26         int next=edge.v;
27         dfs1(next);
28         if(dp[now][0] < dp[next][0]+edge.w) // ( "其某个孩子的最大"+"其与孩子的距离" ) > "最大" > "次大"
29         {
30             dp[now][1] = dp[now][0];
31             dp[now][0] = dp[next][0] + edge.w;
32             idx[now]=next;
33         }
34         else if(dp[now][1] < dp[next][0]+edge.w) // "最大" > ( "其某个孩子的最大"+"其与孩子的距离" ) > "次大"
35         {
36             dp[now][1] = dp[next][0]+edge.w;
37         }
38     }
39 }
40 void dfs2(int now)
41 {
42     for(int i=0;i<child[now].size();i++)
43     {
44         Edge edge=child[now][i];
45         int next=edge.v;
46         if(idx[now]==next) dp[next][2] = edge.w + max(dp[now][1],dp[now][2]);
47         else dp[next][2] = edge.w + max(dp[now][0],dp[now][2]);
48         dfs2(next);
49     }
50 }
51 int main()
52 {
53     while(scanf("%d",&n)!=EOF)
54     {
55         for(int i=0;i<=MAXN;i++) child[i].clear();
56         memset(dp,0,sizeof(dp));
57         memset(idx,0,sizeof(idx));
58         for(int i=2;i<=n;i++)
59         {
60             int father,length;
61             scanf("%d%d",&father,&length);
62             child[father].push_back((Edge){father,i,length});
63         }
64         dfs1(1);
65         dfs2(1);
66         for(int i=1;i<=n;i++)
67         {
68             printf("%d\n",max(dp[i][0],dp[i][2]));
69         }
70     }
71 }

总的来说,这是一道很不错的树形DP题。

时间: 2024-10-10 06:52:50

HDU 2196 - Computer的相关文章

HDU 2196 Computer 经典树形DP

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

HDU 2196 Computer 树形DP经典题

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

HDU 2196 Computer( 树上节点的最远距离 )

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

hdu 2196 computer 树状dp

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

hdu 2196 Computer 树的直径

Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) 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

树形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 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 a

hdu 2196 Computer DP

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

题解报告: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 a