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)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. Codeforces Round #419 (Div. 2) A. Karen and Morning(模拟)

    http://codeforces.com/contest/816/problem/A 题意: 给出一个时间,问最少过多少时间后是回文串. 思路: 模拟,先把小时的逆串计算出来: ① 如果逆串=分钟, ...

  6. 【找规律】【递推】【二项式定理】Codeforces Round #419 (Div. 1) B. Karen and Test

    打个表出来看看,其实很明显. 推荐打这俩组 11 1 10 100 1000 10000 100000 1000000 10000000 100000000 1000000000 1000000000 ...

  7. 【贪心】 Codeforces Round #419 (Div. 1) A. Karen and Game

    容易发现,删除的顺序不影响答案. 所以可以随便删. 如果行数大于列数,就先删列:否则先删行. #include<cstdio> #include<algorithm> usin ...

  8. 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 ...

  9. Codeforces Round #382 (Div. 2) 继续python作死 含树形DP

    A - Ostap and Grasshopper zz题能不能跳到  每次只能跳K步 不能跳到# 问能不能T-G  随便跳跳就可以了  第一次居然跳越界0.0  傻子哦  WA1 n,k = map ...

随机推荐

  1. bootstrap+html5+css3

    一.栅格和块阴影 <!DOCTYPE html> <html> <head> <title>Bootstrap 实例 - 堆叠的水平</title ...

  2. hdu5411 CRB and Puzzle[矩阵优化dp]

    CRB and Puzzle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  3. jquery实现ajax跨域请求!亲测有效

    在解决跨域的时候,我通常会用豆瓣api作为尝试. 下面是本地跨域请求豆瓣API:亲测有效: <script type="text/javascript"> var ur ...

  4. JS通过正则限制 input 输入框只能输入整数、小数(金额或者现金)

    第一: 限制只能是整数 <input type = "text" name= "number" id = 'number' onkeyup= " ...

  5. swoole--PHP的异步、并行、高性能网络通信引擎

    swoole目前已被多家移动互联网.物联网.网络游戏.手机游戏企业使用,替代了C++.Java等复杂编程语言来实现网络服务器程序. 使用PHP+Swoole,开发效率可以大大提升.官方提供了基于swo ...

  6. java 常见几种发送http请求案例

    import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java ...

  7. Uva10917 Walk Through the Forest

    题目链接:https://vjudge.net/problem/UVA-10917 题目意思:Jimmy下班回家要闯过一下森林,劳累一天后在森林中散步是非常惬意的事,所以他打算每天沿着一条不同的路径回 ...

  8. python使用tesseract-ocr完成验证码识别

    全自动区分计算机和人类的公开图灵测试(Completely Automated Public Turing test to tell Computers and Humans Apart) 简称CAP ...

  9. mysql 数据操作 单表查询 where约束 is null in

    需求找出年龄是 81 或者 73 或者 28 mysql ; +----+-----------+--------+-----+------------+-----------+----------- ...

  10. js-jquery-从SweetAlert到SweetAlert2

    原文地址:https://github.com/limonte/sweetalert2/wiki/Migration-from-SweetAlert-to-SweetAlert2 1. IE supp ...