【题目链接】

点击打开链接

【算法】

树形DP

f[i][j]表示以i为根的子树中,选了j个叶子节点,所能带来的最大收益

不难发现这就是一个经典的背包问题,不过是在树上做背包罢了

最后,判断f[1][i]是否大于等于0,输出最大的i

【代码】

  1. #include <algorithm>
  2. #include <bitset>
  3. #include <cctype>
  4. #include <cerrno>
  5. #include <clocale>
  6. #include <cmath>
  7. #include <complex>
  8. #include <cstdio>
  9. #include <cstdlib>
  10. #include <cstring>
  11. #include <ctime>
  12. #include <deque>
  13. #include <exception>
  14. #include <fstream>
  15. #include <functional>
  16. #include <limits>
  17. #include <list>
  18. #include <map>
  19. #include <iomanip>
  20. #include <ios>
  21. #include <iosfwd>
  22. #include <iostream>
  23. #include <istream>
  24. #include <ostream>
  25. #include <queue>
  26. #include <set>
  27. #include <sstream>
  28. #include <stdexcept>
  29. #include <streambuf>
  30. #include <string>
  31. #include <utility>
  32. #include <vector>
  33. #include <cwchar>
  34. #include <cwctype>
  35. #include <stack>
  36. #include <limits.h>
  37. using namespace std;
  38. #define MAXN 3010
  39. const int INF = 2e9;
  40.  
  41. int i,j,a,c,k,x,n,m;
  42. vector< pair<int,int> > e[MAXN];
  43. int size[MAXN],f[MAXN][MAXN];
  44.  
  45. inline void dfs(int x)
  46. {
  47. int i,j,k,y,cost;
  48. for (i = ; i < e[x].size(); i++)
  49. {
  50. y = e[x][i].first;
  51. cost = e[x][i].second;
  52. dfs(y);
  53. size[x] += size[y];
  54. for (j = size[x]; j >= ; j--)
  55. {
  56. for (k = ; k <= size[y]; k++)
  57. {
  58. if (j >= k && f[x][j-k] != -INF && f[y][k] != -INF)
  59. f[x][j] = max(f[x][j],f[x][j-k]+f[y][k]-cost);
  60. }
  61. }
  62. }
  63. }
  64.  
  65. int main()
  66. {
  67.  
  68. scanf("%d%d",&n,&m);
  69. for (i = ; i <= n; i++)
  70. {
  71. for (j = ; j <= m; j++)
  72. {
  73. f[i][j] = -INF;
  74. }
  75. }
  76. for (i = ; i <= n - m; i++)
  77. {
  78. scanf("%d",&k);
  79. for (j = ; j <= k; j++)
  80. {
  81. scanf("%d%d",&a,&c);
  82. e[i].push_back(make_pair(a,c));
  83. }
  84. }
  85.  
  86. for (i = n - m + ; i <= n; i++)
  87. {
  88. scanf("%d",&x);
  89. size[i] = ;
  90. f[i][] = x;
  91. }
  92.  
  93. dfs();
  94.  
  95. for (i = m; i >= ; i--)
  96. {
  97. if (f[][i] >= )
  98. {
  99. printf("%d\n",i);
  100. break;
  101. }
  102. }
  103.  
  104. return ;
  105.  
  106. }

【POJ 1155】TELE的更多相关文章

  1. bzoj 2295: 【POJ Challenge】我爱你啊

    2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec  Memory Limit: 128 MB Description ftiasch是个十分受女生欢迎的同学,所以 ...

  2. 【链表】BZOJ 2288: 【POJ Challenge】生日礼物

    2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 382  Solved: 111[Submit][S ...

  3. BZOJ2288: 【POJ Challenge】生日礼物

    2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 284  Solved: 82[Submit][St ...

  4. BZOJ2293: 【POJ Challenge】吉他英雄

    2293: [POJ Challenge]吉他英雄 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 80  Solved: 59[Submit][Stat ...

  5. BZOJ2287: 【POJ Challenge】消失之物

    2287: [POJ Challenge]消失之物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 254  Solved: 140[Submit][S ...

  6. BZOJ2295: 【POJ Challenge】我爱你啊

    2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 126  Solved: 90[Submit][Sta ...

  7. BZOJ2296: 【POJ Challenge】随机种子

    2296: [POJ Challenge]随机种子 Time Limit: 1 Sec  Memory Limit: 128 MBSec  Special JudgeSubmit: 114  Solv ...

  8. BZOJ2292: 【POJ Challenge 】永远挑战

    2292: [POJ Challenge ]永远挑战 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 513  Solved: 201[Submit][ ...

  9. 【POJ 1125】Stockbroker Grapevine

    id=1125">[POJ 1125]Stockbroker Grapevine 最短路 只是这题数据非常水. . 主要想大牛们试试南阳OJ同题 链接例如以下: http://acm. ...

随机推荐

  1. Epic Moments

    网络流序号要考虑超级源和超级汇 SAP要记得即使还原当前弧 二分图匹配中v.w要取局部变量 RMQ时记得开大数组 树链剖分记得结点要变为线段树中的下标

  2. BZOJ4551 - [TJOI2016]树

    Portal Description 给出一棵\(n(n\leq10^5)\)个点的以\(1\)为根的有根树,进行\(Q(Q\leq10^5)\)次操作: 标记一个点\(x\). 询问\(x\)的祖先 ...

  3. LCA 在线倍增法 求最近公共祖先

    第一步:建树  这个就不说了 第二部:分为两步  分别是深度预处理和祖先DP预处理 DP预处理: int i,j; ;(<<j)<n;j++) ;i<n;++i) ) fa[i ...

  4. 找了两个小时的错误,net.sf.json.JSONException: JSON keys cannot be null.

    因为数据库里面一条记录插入的是NULL,所以导致报了net.sf.json.JSONException: JSON keys cannot be null,找了半天都找不出来问题所在,其他人又都可以启 ...

  5. linux 安装报错:pkg-config not found

    linux 安装报错:pkg-config not found 使用编译安装时,在执行./configure时报如下错误: ... ... checking for pkg-config... no ...

  6. FLEX中restrict限定TextInput输入

    restrict限制的意思 1. 限制某个字符的输入,用符号 ^ 跟上要限制的字符,可跟多个字符  <!-- 限制字符"~"的输入 --> <mx:TextInp ...

  7. POJ 3666 Making the Grade【DP】

    读题堪忧啊,敲完了才发现理解错了..理解题必须看样例啊!! 题目链接: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=110495#pro ...

  8. 2017多校Round7(hdu6120~hdu6132)

    补题进度:9/13 1001 待填坑 1002(数学推导) 题意 有一个按顺序的n个点的k叉树,问每个点子树个数的异或和是多少(n,k<=1e18) 分析 可以先求出最大的d,满足d以上都是满K ...

  9. Java连接MySQL报错:CommunicationsException: Communications link failure

    现象: 报错:Exception in thread "main" com.mysql.cj.jdbc.exceptions.CommunicationsException: Co ...

  10. 编译iOS使用的.a库文件

    首先是须要编译成.a的源文件 hello.h: #ifndef __INCLUDE_HELLO_H__ #define __INCLUDE_HELLO_H__ void hello(const cha ...