HDU 1561 The more, The Better 树形DP
The more, The Better
0 1
0 2
0 3
7 4
2 2
0 1
0 4
2 1
7 1
7 6
2 2
0 0
13
题意:
题解:
显然是有多颗树的
对于一棵树假设根节点是x, 那么我们有size[x] 中选择这个遍历的时候DP即可
对于所有的树,做一遍分组背包就好了
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include<vector>
#include <algorithm>
using namespace std;
const int N = 2e2+, M = 1e2+, mod = 1e9+, inf = 1e9+;
typedef long long ll; int n,m,v[N],f[N],dp[N][N],p[N];
vector<int > G[N];
int siz[N];
void dfs(int u) {
siz[u] = ;
dp[u][] = v[u];
int totson = G[u].size();
for(int i=;i<G[u].size();i++) {
int to = G[u][i];
dfs(to);
siz[u] += siz[to];
}
for(int j=;j<totson;j++) {
int v = G[u][j];
for(int i=siz[u];i>=;i--) {
for(int k=;k<i&&k<=siz[u];k++) {
dp[u][i] = max(dp[v][k]+dp[u][i-k] , dp[u][i]);
}
}
}
}
int main() {
while(~scanf("%d%d",&n,&m)) {
if(!n&&!m) break;
int cnt = ;
for(int i=;i<N;i++) G[i].clear();
memset(dp,,sizeof(dp));
memset(f,,sizeof(f));
for(int i=;i<=n;i++) {
int a,b;
scanf("%d%d",&a,&v[i]);
if(a == ) {p[++cnt] = i;continue;}
G[a].push_back(i);
}
for(int i=;i<=cnt;i++) dfs(p[i]);
for(int i=;i<=cnt;i++) {
for(int x=m;x>=;x--)
for(int j=;j<=siz[p[i]]&&j<=x;j++) {
f[x] = max(f[x-j] + dp[p[i]][j],f[x]);
}
}
printf("%d\n",f[m]); }
}
HDU 1561 The more, The Better 树形DP的更多相关文章
- hdu 1561 The more, The Better(树形dp,基础)
The more, The Better Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- HDU 1520.Anniversary party 基础的树形dp
Anniversary party Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- HDU 1561The more, The Better(树形DP)
HDU 1561 The more, The Better 题目大意就不说了 直接DP[i][j]表示i为跟节点的子树上攻克j个城堡的所能获得的最多宝物的数量 DP[fa][j] = MAX{DP[ ...
- HDU 3586 Information Disturbing(二分+树形dp)
http://acm.split.hdu.edu.cn/showproblem.php?pid=3586 题意: 给定一个带权无向树,要切断所有叶子节点和1号节点(总根)的联系,每次切断边的费用不能超 ...
- HDU 5682 zxa and leaf 二分 树形dp
zxa and leaf 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5682 Description zxa have an unrooted t ...
- HDU 6201 2017沈阳网络赛 树形DP或者SPFA最长路
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6201 题意:给出一棵树,每个点有一个权值,代表商品的售价,树上每一条边上也有一个权值,代表从这条边经过 ...
- hdu 4612 Warm up 双连通+树形dp思想
Warm up Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) Total S ...
- HDU 1054 Strategic Game(最小点覆盖+树形dp)
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=106048#problem/B 题意:给出一些点相连,找出最小的点数覆盖所有的 ...
- HDU 5977 Garden of Eden (树形dp+快速沃尔什变换FWT)
CGZ大佬提醒我,我要是再不更博客可就连一月一更的频率也没有了... emmm,正好做了一道有点意思的题,就拿出来充数吧=.= 题意 一棵树,有 $ n (n\leq50000) $ 个节点,每个点都 ...
随机推荐
- FileUpload1上传控件
<asp:FileUpload ID="FileUpload1" runat="server" /> string fn = System.IO.P ...
- offset求结构体成员的偏移量
[代码] C++ Code 12345678910111213141516171819202122232425262728293031 /* version: 1.0 author: hell ...
- CentOS 7部署flume
CentOS 7部署flume 准备工作: 安装java并设置java环境变量,在`/etc/profile`中加入 export JAVA_HOME=/usr/java/jdk1.8.0_65 ex ...
- QQ右下角图标不见了
[QQ]我的qq是在线的,但是桌面右下角的企鹅小图标却不见了??? 最好的办法是:CTRL+ALT+Z,先把QQ的控制面板调出来 然后点菜单,选设置,系统设置.在基本设置的选项卡中,窗口设置的最后一项 ...
- Effective C++ -----条款32:确定你的public继承塑模出is-a关系
“public继承”意味is-a.适用于base classes身上的每一件事情一定也适用于derived classes身上,因为每一个derive class对象也都是一个base class对象 ...
- nyoj_31
题目描述:五个数求最值. #include <iostream> #include <algorithm> using namespace std; int main(){ ] ...
- Divide and conquer:4 Values whose Sum is 0(POJ 2785)
找四个数的和为0 题目大意:给定四个集合,要你每个集合选4个数字,组成和为0 这题是3977的简单版,只要和是0就可以了 #include <iostream> #include < ...
- 【leetcode】Longest Consecutive Sequence(hard)☆
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- codeforces gym 100286 I iSharp (字符串模拟)
题目链接 给定一个字符串.输入是int& a*[]&, b, c*; 输出是 int&&[]* a;int& b;int&* c; 输入格式里逗号后面一 ...
- UISearchController Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior
Attempting to load the view of a view controller while it is deallocating is not allowed and may res ...