现在的位置: 首页 > 综合 > 正文

HDU2196——–树上DP

2017年12月15日 ⁄ 综合 ⁄ 共 2412字 ⁄ 字号 评论关闭
分类: 动态规划 97人阅读 评论(0) 收藏 举报

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=2196

题目意思:

给你一个无向图,但是没有环,因为有N个点,只有N-1条边

要你求出N个点在这个图上能够走的最远距离

解题思路:

这是赤裸裸的无环的无向图,我们可以把它转化为一棵树

其实以任意一个节点作为树根都是可以的,做N遍DFS就可以

但是会超时,所以我们要想办法解决超时的问题,也就是减少DFS的次数

我们可以先做一遍DFS,求出每个节点到自己子树上的最远距离,这个可以通过类似回溯的方法,但是我们保存在数组中了

然后再第二次DFS的时候,我们根据之前的DFS可以求出每个节点的父节点不经过自己的最远距离

然后将这些一起比较就可以求出来每个节点所能走的最远距离

下面上代码:

  1. #include<cstdio>  
  2. #include<cstring>  
  3. #include<algorithm>  
  4. #include<cmath>  
  5. #include<vector>  
  6. using namespace std;  
  7.   
  8. struct node  
  9. {  
  10.     int v;  
  11.     int len;  
  12. };  
  13.   
  14. const int maxn = 10005;  
  15. const int inf = 0x3f3f3f3f;  
  16. int dp[maxn];  
  17. int n;  
  18. vector<node> computer[maxn];  
  19.   
  20. void read_init()  
  21. {  
  22.     for(int i=0;i<=n;i++)  
  23.         computer[i].clear();  
  24.     for(int i=1;i<=n;i++)  
  25.         dp[i] = 0;  
  26.     int a,b;  
  27.     node tmp;  
  28.   
  29.     for(int i=2;i<=n;i++)  
  30.     {  
  31.         scanf("%d%d",&a,&b);  
  32.         tmp.v=a;  
  33.         tmp.len=b;  
  34.         computer[i].push_back(tmp);  
  35.         tmp.v=i;  
  36.         tmp.len=b;  
  37.         computer[a].push_back(tmp);  
  38.     }  
  39. }  
  40.   
  41. void dfs_to_son(int root,int fa)  
  42. {  
  43.     for(int i=0;i<computer[root].size();i++)  
  44.     {  
  45.         int v = computer[root][i].v;  
  46.         if(v==fa)  
  47.             continue;  
  48.         dfs_to_son(v,root);  
  49.         dp[root] = max(dp[root],computer[root][i].len+dp[v]);  
  50.         computer[root][i].len = computer[root][i].len+dp[v];  
  51.     }  
  52. }  
  53.   
  54. void dfs_find_longest_path(int root,int fa)  
  55. {  
  56.     int not_son_longest=0;  
  57.     for(int i=0;i<computer[fa].size();i++)  
  58.     {  
  59.         int v = computer[fa][i].v;  
  60.         if(v==root)  
  61.             continue;  
  62.         not_son_longest = max(not_son_longest,computer[fa][i].len);  
  63.     }  
  64.   
  65.     for(int i=0;i<computer[root].size();i++)  
  66.     {  
  67.         int v = computer[root][i].v;  
  68.         if(v==fa)  
  69.         {  
  70.             computer[root][i].len = not_son_longest+computer[root][i].len;  
  71.             break;  
  72.         }  
  73.     }  
  74.   
  75.     for(int i=0;i<computer[root].size();i++)  
  76.     {  
  77.         dp[root] = max(dp[root],computer[root][i].len);  
  78.     }  
  79.   
  80.     for(int i=0;i<computer[root].size();i++)  
  81.     {  
  82.         int v = computer[root][i].v;  
  83.         if(v==fa)  
  84.             continue;  
  85.         dfs_find_longest_path(v,root);  
  86.     }  
  87. }  
  88.   
  89. int main()  
  90. {  
  91.     while(~scanf("%d",&n))  
  92.     {  
  93.         read_init();  
  94.         dfs_to_son(1,0);  
  95.         dfs_find_longest_path(1,0);  
  96.         for(int i=1;i<=n;i++)  
  97.             printf("%d\n",dp[i]);  
  98.     }  
  99.     return 0;  
  100. }  

抱歉!评论已关闭.