Codeforces Round #419 (Div. 2) E. Karen and Supermarket(树形dp)
http://codeforces.com/contest/816/problem/E
题意:
去超市买东西,共有m块钱,每件商品有优惠卷可用,前提是xi商品的优惠券被用。问最多能买多少件商品?
思路:
第一件商品使用优惠券不需要前提,别的都是需要的,然后这样就形成了一棵以1为根的树。
这样,很容易想到是树形dp。
d【u】【j】【0/1】表示以u为根的子数中选择j件商品所需的最少花费,0/1表示u商品是否能用优惠券。
解释一下代码中的sz【】,它所代表的是以u为根的子树的结点数。

当我们现在访问的是1号结点时,sz【1】=1,然后访问2号结点,2号结点访问结束后,我们就会重新回到1号结点的dfs处,然后进行状态转移
for(int j=sz[u];j>=;j--) //表示sz【u】中选取的j个个数 for(int k=;k<=sz[v];k++) //表示v结点及其子树中选取的个数
最后将sz【2】的值加到sz【1】中,这样等下一次进行结点3的状态转移时,只需要考虑在2的子树中选取多少个和3的子树中选取多少个即可。到4时,只需要考虑在2和3中一共选取多少个和4的子树中选取多少个,因为前者在上一步已经计算出了最小花费。
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<sstream>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int,int> pll;
const int INF = 0x3f3f3f3f;
const int maxn = +; int n, m; int pa[maxn], pb[maxn];
int sz[maxn];
int d[maxn][maxn][]; vector<int> g[maxn]; void dfs(int u)
{
sz[u]=;
d[u][][]=, d[u][][]=pa[u], d[u][][]=pa[u]-pb[u];
for(int i=;i<g[u].size();i++)
{
int v=g[u][i];
dfs(v); for(int j=sz[u];j>=;j--)
{
for(int k=;k<=sz[v];k++)
{
d[u][j+k][]=min(d[u][j+k][],d[u][j][]+d[v][k][]);
d[u][j+k][]=min(d[u][j+k][],min(d[u][j][]+d[v][k][],d[u][j][]+d[v][k][]));
}
}
sz[u]+=sz[v];
}
} int main()
{
//freopen("in.txt","r",stdin);
while(~scanf("%d%d",&n, &m))
{
for(int i=;i<=n;i++) g[i].clear();
for(int i=;i<=n;i++)
{
scanf("%d%d",&pa[i],&pb[i]);
if(i>)
{
int x;
scanf("%d",&x);
g[x].push_back(i);
}
} memset(d,INF,sizeof(d));
dfs(); int i;
for(i=;i<=n;i++)
{
if(min(d[][i][],d[][i][])>m) break;
}
printf("%d\n",i-);
}
return ;
}
Codeforces Round #419 (Div. 2) E. Karen and Supermarket(树形dp)的更多相关文章
- Codeforces Round #419 (Div. 1) C. Karen and Supermarket 树形DP
C. Karen and Supermarket On the way home, Karen decided to stop by the supermarket to buy some g ...
- Codeforces Round #419 (Div. 2) B. Karen and Coffee(经典前缀和)
http://codeforces.com/contest/816/problem/B To stay woke and attentive during classes, Karen needs s ...
- Codeforces Round #419 (Div. 2) C. Karen and Game
C. Karen and Game time limit per test 2 seconds memory limit per test 512 megabytes input standard i ...
- Codeforces Round #419 (Div. 2) B. Karen and Coffee
To stay woke and attentive during classes, Karen needs some coffee! Karen, a coffee aficionado, want ...
- Codeforces Round #419 (Div. 2) A. Karen and Morning(模拟)
http://codeforces.com/contest/816/problem/A 题意: 给出一个时间,问最少过多少时间后是回文串. 思路: 模拟,先把小时的逆串计算出来: ① 如果逆串=分钟, ...
- 【找规律】【递推】【二项式定理】Codeforces Round #419 (Div. 1) B. Karen and Test
打个表出来看看,其实很明显. 推荐打这俩组 11 1 10 100 1000 10000 100000 1000000 10000000 100000000 1000000000 1000000000 ...
- 【贪心】 Codeforces Round #419 (Div. 1) A. Karen and Game
容易发现,删除的顺序不影响答案. 所以可以随便删. 如果行数大于列数,就先删列:否则先删行. #include<cstdio> #include<algorithm> usin ...
- Codeforces Round #196 (Div. 2) D. Book of Evil 树形dp
题目链接: http://codeforces.com/problemset/problem/337/D D. Book of Evil time limit per test2 secondsmem ...
- Codeforces Round #382 (Div. 2) 继续python作死 含树形DP
A - Ostap and Grasshopper zz题能不能跳到 每次只能跳K步 不能跳到# 问能不能T-G 随便跳跳就可以了 第一次居然跳越界0.0 傻子哦 WA1 n,k = map ...
随机推荐
- 【BZOJ1692】[Usaco2007 Dec]队列变换 后缀数组+贪心
[BZOJ1692][Usaco2007 Dec]队列变换 Description FJ打算带他的N(1 <= N <= 30,000)头奶牛去参加一年一度的“全美农场主大奖赛”.在这场比 ...
- oneThink添加成功,返回到当前请求地址!
其实没什么,就一行代码: $this->success('已采纳',$_SERVER['HTTP_REFERER']);
- 最小树形图(poj3164)
Command Network Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 12834 Accepted: 3718 ...
- Spring项目对JDBC的支持和基本使用
欢迎查看Java开发之上帝之眼系列教程,如果您正在为Java后端庞大的体系所困扰,如果您正在为各种繁出不穷的技术和各种框架所迷茫,那么本系列文章将带您窥探Java庞大的体系.本系列教程希望您能站在上帝 ...
- Tomcat 400错误 问题集锦
1.前后台参数类型不一致 上图错误提示就是客户端发送的请求不能找到你的具体的页面或者地址,这是Spring MVC抛出的错误,这样我们就要进行参数的检查,一定是JSP提交的参数和Controller里 ...
- mysql : show processlist 详解
最近排查一些MySQL的问题,会经常用到 show processlist,所以在这里把这个命令总结一下,做个备忘,以备不时只需. 首先是几条常用的SQL. 1.按客户端 IP 分组,看哪个客户端的链 ...
- ibatis 中#和 $ 符号的区别
1.数据类型匹配 #:会进行预编译,而且进行类型匹配(自动确定数据类型): $:不进行数据类型匹配. 2.实现方式: # 用于变量替换(先生成一个占位符,然后替换) select * from use ...
- Flum入门必备知识
1.flume概念 flume是分布式的,可靠的,高可用的,用于对不同来源的大量的日志数据进行有效收集.聚集和移动,并以集中式的数据存储的系统. flume目前是apache的一个顶级项目. flum ...
- pandas的replace方法
就是将一个值替换为另一个值,以前我用的是赋值方式,这里应该效率会高. 1.说明: 语法:replace(self, to_replace=None, value=None, inplace=False ...
- JS实现数字千位符格式化方法
/** * [number_format 参数说明:] * @param {[type]} number [number:要格式化的数字] * @param {[type]} decimals [de ...