【POJ 1155】TELE
【题目链接】
【算法】
树形DP
f[i][j]表示以i为根的子树中,选了j个叶子节点,所能带来的最大收益
不难发现这就是一个经典的背包问题,不过是在树上做背包罢了
最后,判断f[1][i]是否大于等于0,输出最大的i
【代码】
- #include <algorithm>
- #include <bitset>
- #include <cctype>
- #include <cerrno>
- #include <clocale>
- #include <cmath>
- #include <complex>
- #include <cstdio>
- #include <cstdlib>
- #include <cstring>
- #include <ctime>
- #include <deque>
- #include <exception>
- #include <fstream>
- #include <functional>
- #include <limits>
- #include <list>
- #include <map>
- #include <iomanip>
- #include <ios>
- #include <iosfwd>
- #include <iostream>
- #include <istream>
- #include <ostream>
- #include <queue>
- #include <set>
- #include <sstream>
- #include <stdexcept>
- #include <streambuf>
- #include <string>
- #include <utility>
- #include <vector>
- #include <cwchar>
- #include <cwctype>
- #include <stack>
- #include <limits.h>
- using namespace std;
- #define MAXN 3010
- const int INF = 2e9;
- int i,j,a,c,k,x,n,m;
- vector< pair<int,int> > e[MAXN];
- int size[MAXN],f[MAXN][MAXN];
- inline void dfs(int x)
- {
- int i,j,k,y,cost;
- for (i = ; i < e[x].size(); i++)
- {
- y = e[x][i].first;
- cost = e[x][i].second;
- dfs(y);
- size[x] += size[y];
- for (j = size[x]; j >= ; j--)
- {
- for (k = ; k <= size[y]; k++)
- {
- if (j >= k && f[x][j-k] != -INF && f[y][k] != -INF)
- f[x][j] = max(f[x][j],f[x][j-k]+f[y][k]-cost);
- }
- }
- }
- }
- int main()
- {
- scanf("%d%d",&n,&m);
- for (i = ; i <= n; i++)
- {
- for (j = ; j <= m; j++)
- {
- f[i][j] = -INF;
- }
- }
- for (i = ; i <= n - m; i++)
- {
- scanf("%d",&k);
- for (j = ; j <= k; j++)
- {
- scanf("%d%d",&a,&c);
- e[i].push_back(make_pair(a,c));
- }
- }
- for (i = n - m + ; i <= n; i++)
- {
- scanf("%d",&x);
- size[i] = ;
- f[i][] = x;
- }
- dfs();
- for (i = m; i >= ; i--)
- {
- if (f[][i] >= )
- {
- printf("%d\n",i);
- break;
- }
- }
- return ;
- }
【POJ 1155】TELE的更多相关文章
- bzoj 2295: 【POJ Challenge】我爱你啊
2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec Memory Limit: 128 MB Description ftiasch是个十分受女生欢迎的同学,所以 ...
- 【链表】BZOJ 2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 382 Solved: 111[Submit][S ...
- BZOJ2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 284 Solved: 82[Submit][St ...
- BZOJ2293: 【POJ Challenge】吉他英雄
2293: [POJ Challenge]吉他英雄 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 80 Solved: 59[Submit][Stat ...
- BZOJ2287: 【POJ Challenge】消失之物
2287: [POJ Challenge]消失之物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 254 Solved: 140[Submit][S ...
- BZOJ2295: 【POJ Challenge】我爱你啊
2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 126 Solved: 90[Submit][Sta ...
- BZOJ2296: 【POJ Challenge】随机种子
2296: [POJ Challenge]随机种子 Time Limit: 1 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 114 Solv ...
- BZOJ2292: 【POJ Challenge 】永远挑战
2292: [POJ Challenge ]永远挑战 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 513 Solved: 201[Submit][ ...
- 【POJ 1125】Stockbroker Grapevine
id=1125">[POJ 1125]Stockbroker Grapevine 最短路 只是这题数据非常水. . 主要想大牛们试试南阳OJ同题 链接例如以下: http://acm. ...
随机推荐
- Epic Moments
网络流序号要考虑超级源和超级汇 SAP要记得即使还原当前弧 二分图匹配中v.w要取局部变量 RMQ时记得开大数组 树链剖分记得结点要变为线段树中的下标
- BZOJ4551 - [TJOI2016]树
Portal Description 给出一棵\(n(n\leq10^5)\)个点的以\(1\)为根的有根树,进行\(Q(Q\leq10^5)\)次操作: 标记一个点\(x\). 询问\(x\)的祖先 ...
- LCA 在线倍增法 求最近公共祖先
第一步:建树 这个就不说了 第二部:分为两步 分别是深度预处理和祖先DP预处理 DP预处理: int i,j; ;(<<j)<n;j++) ;i<n;++i) ) fa[i ...
- 找了两个小时的错误,net.sf.json.JSONException: JSON keys cannot be null.
因为数据库里面一条记录插入的是NULL,所以导致报了net.sf.json.JSONException: JSON keys cannot be null,找了半天都找不出来问题所在,其他人又都可以启 ...
- linux 安装报错:pkg-config not found
linux 安装报错:pkg-config not found 使用编译安装时,在执行./configure时报如下错误: ... ... checking for pkg-config... no ...
- FLEX中restrict限定TextInput输入
restrict限制的意思 1. 限制某个字符的输入,用符号 ^ 跟上要限制的字符,可跟多个字符 <!-- 限制字符"~"的输入 --> <mx:TextInp ...
- POJ 3666 Making the Grade【DP】
读题堪忧啊,敲完了才发现理解错了..理解题必须看样例啊!! 题目链接: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=110495#pro ...
- 2017多校Round7(hdu6120~hdu6132)
补题进度:9/13 1001 待填坑 1002(数学推导) 题意 有一个按顺序的n个点的k叉树,问每个点子树个数的异或和是多少(n,k<=1e18) 分析 可以先求出最大的d,满足d以上都是满K ...
- Java连接MySQL报错:CommunicationsException: Communications link failure
现象: 报错:Exception in thread "main" com.mysql.cj.jdbc.exceptions.CommunicationsException: Co ...
- 编译iOS使用的.a库文件
首先是须要编译成.a的源文件 hello.h: #ifndef __INCLUDE_HELLO_H__ #define __INCLUDE_HELLO_H__ void hello(const cha ...