题意:

一个树求得到一个节点数为p的子树,最小需要删除的边数。

分析:父节点到儿子这条边,删或不删,背包问题。

  1. #include <map>
  2. #include <set>
  3. #include <list>
  4. #include <cmath>
  5. #include <queue>
  6. #include <stack>
  7. #include <cstdio>
  8. #include <vector>
  9. #include <string>
  10. #include <cctype>
  11. #include <complex>
  12. #include <cassert>
  13. #include <utility>
  14. #include <cstring>
  15. #include <cstdlib>
  16. #include <iostream>
  17. #include <algorithm>
  18. using namespace std;
  19. typedef pair<int,int> PII;
  20. typedef long long ll;
  21. #define lson l,m,rt<<1
  22. #define pi acos(-1.0)
  23. #define rson m+1,r,rt<<11
  24. #define All 1,N,1
  25. #define read freopen("in.txt", "r", stdin)
  26. const ll INFll = 0x3f3f3f3f3f3f3f3fLL;
  27. const int INF= 0x7ffffff;
  28. const int mod = ;
  29. int dp[][],n,p,par[];
  30. int find(int x){
  31. return x==par[x]?x:par[x]=find(par[x]);
  32. }
  33. vector<int>e[];
  34. void dfs(int root){
  35. for(int i=;i<=p;++i)
  36. dp[root][i]=INF;
  37. dp[root][]=;
  38. for(int i=;i<e[root].size();++i){
  39. int son=e[root][i];
  40. dfs(son);
  41. for(int j=p;j>;--j)
  42. for(int k=;k<j;++k){
  43. if(k==)dp[root][j]=dp[root][j]+;//删除边数加1
  44. else
  45. dp[root][j]=min(dp[root][j],dp[root][j-k]+dp[son][k]);//不删背包
  46. }
  47. }
  48. }
  49. int main()
  50. {
  51. while(~scanf("%d%d",&n,&p)){
  52. for(int i=;i<=n;++i){
  53. par[i]=i;
  54. e[i].clear();
  55. }
  56. int a,b;
  57. for(int i=;i<n-;++i)
  58. {
  59. scanf("%d%d",&a,&b);
  60. e[a].push_back(b);
  61. par[b]=a;
  62. }
  63. int root=find();
  64. dfs(root);
  65. int minn=dp[root][p];
  66. for(int i=;i<=n;++i){
  67. minn=min(minn,dp[i][p]+);
  68. }
  69. printf("%d\n",minn);
  70. }
  71. return ;
  72. }

POJ 1947-Rebuilding Roads(树形背包)的更多相关文章

  1. [poj 1947] Rebuilding Roads 树形DP

    Rebuilding Roads Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 10653 Accepted: 4884 Des ...

  2. DP Intro - poj 1947 Rebuilding Roads(树形DP)

    版权声明:本文为博主原创文章,未经博主允许不得转载. Rebuilding Roads Time Limit: 1000MS   Memory Limit: 30000K Total Submissi ...

  3. POJ 1947 Rebuilding Roads 树形DP

    Rebuilding Roads   Description The cows have reconstructed Farmer John's farm, with its N barns (1 & ...

  4. POJ 1947 Rebuilding Roads 树形dp 难度:2

    Rebuilding Roads Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 9105   Accepted: 4122 ...

  5. POJ 1947 Rebuilding Roads

    树形DP..... Rebuilding Roads Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 8188 Accepted: ...

  6. POJ1947 Rebuilding Roads[树形背包]

    Rebuilding Roads Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 11495   Accepted: 5276 ...

  7. POJ 1947 Rebuilding Roads (树dp + 背包思想)

    题目链接:http://poj.org/problem?id=1947 一共有n个节点,要求减去最少的边,行号剩下p个节点.问你去掉的最少边数. dp[u][j]表示u为子树根,且得到j个节点最少减去 ...

  8. 树形dp(poj 1947 Rebuilding Roads )

    题意: 有n个点组成一棵树,问至少要删除多少条边才能获得一棵有p个结点的子树? 思路: 设dp[i][k]为以i为根,生成节点数为k的子树,所需剪掉的边数. dp[i][1] = total(i.so ...

  9. POJ 1947 - Rebuilding Roads 树型DP(泛化背包转移)..

    dp[x][y]表示以x为根的子树要变成有y个点..最少需要减去的边树... 最终ans=max(dp[i][P]+t)  < i=(1,n) , t = i是否为整棵树的根 > 更新的时 ...

  10. POJ 1947 Rebuilding Roads(树形DP)

    题目链接 题意 : 给你一棵树,问你至少断掉几条边能够得到有p个点的子树. 思路 : dp[i][j]代表的是以i为根的子树有j个节点.dp[u][i] = dp[u][j]+dp[son][i-j] ...

随机推荐

  1. Mapped Statements collection does not contain value for TaskMapper.selectByPrimaryKey

    Mapped Statements collection does not contain value for后面是什么类什么方法之类的: 错误原因有几种: 1.mapper.xml中没有加入name ...

  2. Begin Andriod -- 安装android开发环境

    很久以前学过Andriod,现在已经忘的快没有了,重新捡起来练练,顺带写写博客,感受下写博的乐趣. 第一步:安装java jdk.jre(jdk:开发环境,jre:运行环境). (一)java jdk ...

  3. C#和Js 编码和解码方法

    Server.UrlDecode(); Server.UrlEncode(); Server.HtmlDecode(); Server.HtmlEncode();

  4. myeclipse2013 for linux及其破解补丁百度网盘下载

    FQ下载1.1G的东西不是开玩笑的,用GA下载了两回均失败,还是用了某某门在win下下载好的,来之不易,所以特意上传分享给大家,免得FQ.破解文件也一并附上: 注意:本人这个是在原文件基础上bzip2 ...

  5. js中批量处理样式——cssText的使用

    http://www.cnblogs.com/snandy/archive/2011/03/12/1980444.html

  6. Educational Codeforces Round 5 A

    Problem A:http://codeforces.com/contest/616/problem/A A. Comparing Two Long Integers 果然还是我太天真了(长整数比较 ...

  7. hdu 3449

    有依赖的背包,转化成01背包来做: #include<iostream> #include<cstdio> #include<cstring> #include&l ...

  8. dijkstra,bellman-ford,floyd分析比较

    http://www.cnblogs.com/mengxm-lincf/archive/2012/02/11/2346288.html 其实我一直存在疑惑是什么导致dijkstra不能处理负权图? 今 ...

  9. Oracle----dual

    1. dual is a table that contains a single row. 2. The dual table has one VARCHAR2 column named dummy ...

  10. 《Introduction to Tornado》中文翻译计划——第五章:异步Web服务

    http://www.pythoner.com/294.html 本文为<Introduction to Tornado>中文翻译,将在https://github.com/alioth3 ...