题目传送门

Karen and Supermarket

On the way home, Karen decided to stop by the supermarket to buy some groceries.

She needs to buy a lot of goods, but since she is a student her budget is still quite limited. In fact, she can only spend up to b dollars.

The supermarket sells n goods. The i-th good can be bought for ci dollars. Of course, each good can only be bought once.

Lately, the supermarket has been trying to increase its business. Karen, being a loyal customer, was given n coupons. If Karen purchases the i-th good, she can use the i-th coupon to decrease its price by di. Of course, a coupon cannot be used without buying the corresponding good.

There is, however, a constraint with the coupons. For all i ≥ 2, in order to use the i-th coupon, Karen must also use the xi-th coupon (which may mean using even more coupons to satisfy the requirement for that coupon).

Karen wants to know the following. What is the maximum number of goods she can buy, without exceeding her budget b?

Input

The first line of input contains two integers n and b (1 ≤ n ≤ 5000, 1 ≤ b ≤ 109), the number of goods in the store and the amount of money Karen has, respectively.

The next n lines describe the items. Specifically:

  • The i-th line among these starts with two integers, ci and di (1 ≤ di < ci ≤ 109), the price of the i-th good and the discount when using the coupon for the i-th good, respectively.
  • If i ≥ 2, this is followed by another integer, xi (1 ≤ xi < i), denoting that the xi-th coupon must also be used before this coupon can be used.
Output

Output a single integer on a line by itself, the number of different goods Karen can buy, without exceeding her budget.

Examples
input

Copy
6 16
10 9
10 5 1
12 2 1
20 18 3
10 2 3
2 1 5
output

Copy
4
input

Copy
5 10
3 1
3 1 1
3 1 2
3 1 3
3 1 4
output

Copy
5
Note

In the first test case, Karen can purchase the following 4 items:

  • Use the first coupon to buy the first item for 10 - 9 = 1 dollar.
  • Use the third coupon to buy the third item for 12 - 2 = 10 dollars.
  • Use the fourth coupon to buy the fourth item for 20 - 18 = 2 dollars.
  • Buy the sixth item for 2 dollars.

The total cost of these goods is 15, which falls within her budget. Note, for example, that she cannot use the coupon on the sixth item, because then she should have also used the fifth coupon to buy the fifth item, which she did not do here.

In the second test case, Karen has enough money to use all the coupons and purchase everything.


  分析:

  考试的时候遇到这道题结果还是一脸懵逼,果然我$DP$还是太弱了,还是得好好补补。

  考虑用树形$DP$,根据题意构建出一棵树,然后从根节点开始深搜,把每个节点遍历一遍,然后从叶子节点开始向根节点转移状态。

  定义$f[x][j][0/1]$表示遍历到了第$x$个商品时已经购买了$j$个商品,$0/1$表示第$x$个商品是否打折。

  那么状态转移方程为:

  $f[x][j+k][0]=Min(f[x][j+k][0],f[x][j][0]+f[y][k][0]);$

  $f[x][j+k][1]=Min(f[x][j+k][1],f[x][j][1]+f[y][k][0]);$

  $f[x][j+k][1]=Min(f[x][j+k][1],f[x][j][1]+f[y][k][1]);$

  其中$j$和$k$分别表示当前节点和其子节点已经搜过的子树中购买的商品个数。可能有点绕,可以结合代码理解。

  Code:

//It is made by HolseLee on 15th Aug 2018
//CF815C
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<iomanip>
#include<algorithm>
#define Max(a,b) (a)>(b)?(a):(b)
#define Min(a,b) (a)<(b)?(a):(b)
#define Swap(a,b) (a)^=(b)^=(a)^=(b)
using namespace std; const int N=;
int n,m,head[N],siz,w[N],c[N],rt[N];
long long f[N][N][];
struct Node{
int to,nxt;
}edge[N<<]; inline int read()
{
char ch=getchar();int num=;bool flag=false;
while(ch<''||ch>''){
if(ch=='-')flag=true;ch=getchar();
}
while(ch>=''&&ch<=''){
num=num*+ch-'';ch=getchar();
}
return flag?-num:num;
} inline void add(int x,int y)
{
edge[++siz].to=y;
edge[siz].nxt=head[x];
head[x]=siz;
} void dfs(int x)
{
rt[x]=;f[x][][]=;
f[x][][]=w[x];f[x][][]=c[x];
for(int i=head[x];i;i=edge[i].nxt){
int y=edge[i].to;
dfs(y);
for(int j=rt[x];j>=;--j)
for(int k=;k<=rt[y];++k){
f[x][j+k][]=Min(f[x][j+k][],f[x][j][]+f[y][k][]);
f[x][j+k][]=Min(f[x][j+k][],f[x][j][]+f[y][k][]);
f[x][j+k][]=Min(f[x][j+k][],f[x][j][]+f[y][k][]);
}
rt[x]+=rt[y];
}
} int main()
{
//freopen("shopping.in","r",stdin);
//freopen("shopping.out","w",stdout);
n=read();m=read();
memset(f,/,sizeof(f));
w[]=read();c[]=read();c[]=w[]-c[];
int x,y,z;
for(int i=;i<=n;++i){
x=read();y=read();z=read();
w[i]=x;c[i]=x-y;
add(z,i);
}
dfs();
for(int i=n;i>=;--i){
if(f[][i][]<=m||f[][i][]<=m){
printf("%d\n",i);
break;
}
}
return ;
}

CF815C Karen and Supermarket [树形DP]的更多相关文章

  1. Codeforces 815C Karen and Supermarket 树形dp

    Karen and Supermarket 感觉就是很普通的树形dp. dp[ i ][ 0 ][ u ]表示在 i 这棵子树中选择 u 个且 i 不用优惠券的最小花费. dp[ i ][ 1 ][ ...

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

  3. 816E. Karen and Supermarket 树形DP

    LINK 题意:给出n个商品,除第一个商品外,所有商品可以选择使用优惠券,但要求其前驱商品已被购买,问消费k以下能买几个不同的商品 思路:题意很明显就是树形DP.对于一个商品有三种选择,买且使用优惠券 ...

  4. CF815C Karen and Supermarket

    题目链接 CF815C Karen and Supermarket 题解 只要在最大化数量的前提下,最小化花费就好了 这个数量枚举ok, dp[i][j][1/0]表示节点i的子树中买了j件商品 i ...

  5. [CF816E] Karen and Supermarket1 [树形dp]

    传送门 - > \(CF816E\) Karen and Supermarket 题意翻译 在回家的路上,凯伦决定到超市停下来买一些杂货. 她需要买很多东西,但因为她是学生,所以她的预算仍然很有 ...

  6. Codeforces Round #419 (Div. 2) E. Karen and Supermarket(树形dp)

    http://codeforces.com/contest/816/problem/E 题意: 去超市买东西,共有m块钱,每件商品有优惠卷可用,前提是xi商品的优惠券被用.问最多能买多少件商品? 思路 ...

  7. codeforces 816 E. Karen and Supermarket(树形dp)

    题目链接:http://codeforces.com/contest/816/problem/E 题意:有n件商品,每件有价格ci,优惠券di,对于i>=2,使用di的条件为:xi的优惠券需要被 ...

  8. CodeForces 816E Karen and Supermarket ——(树形DP)

    题意:有n件商品,每件商品都最多只能被买一次,且有一个原价和一个如果使用优惠券以后可以减少的价格,同时,除了第一件商品以外每件商品都有一个xi属性,表示买这个商品时如果要使用优惠券必须已经使用了xi的 ...

  9. E. Karen and Supermarket

    E. Karen and Supermarket time limit per test 2 seconds memory limit per test 512 megabytes input sta ...

随机推荐

  1. JS事件大全及兼容

    一般事件 事件 浏览器支持 描述 onClick IE3|N2|O3 鼠标点击事件,多用在某个对象控制的范围内的鼠标点击 onDblClick IE4|N4|O 鼠标双击事件 onMouseDown  ...

  2. 数学:A^B的约数(因子)之和对MOD取模

    POJ1845 首先把A写成唯一分解定理的形式 分解时让A对所有质数从小到大取模就好了 然后就有:A = p1^k1 * p2^k2 * p3^k3 *...* pn^kn 然后有: A^B = p1 ...

  3. Elasticsearch Java API 配置测试

    Elasticsearch1.X,2.X,5.X随着版本的迭代,除了系统升级,Java API也做了相对较大的调整,也就是说,1.X的API在2.X以及5.X乃至未来6.X版本都不是通用的. 本例子使 ...

  4. errno错误号含义

    errno0 : Success errno1 : Operation not permitted errno2 : No such file or directory errno3 : No suc ...

  5. Flask---使用Bootstrap新建第一个demo

    Flask---使用Bootstrap新建第一个demo 参考自http://www.jianshu.com/p/417bcbad82fb 还有<Flask web开发> 前端用到Boot ...

  6. 关于Http协议、ASP.NET 核心知识(2)

    简介HTTP (对于http协议的描述我前部分有写,但基于保证文档独立完整性的原则,我再写一遍.反正又不花钱.) 这货的学名叫:超文本传输协议 英文名字:(HTTP,HyperText Transfe ...

  7. 【洛谷 P3690】 【模板】Link Cut Tree (动态树)

    题目链接 \(RT\). FlashHu巨佬的博客 #include <cstdio> #define R register int #define I inline void #defi ...

  8. 【leetcode 简单】第三十八题 两数之和 II - 输入有序数组

    给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值index1 和 index2,其中 index1 必须小于 index2. 说明: 返回的下标值( ...

  9. bzoj 3522 tree-dp 暴力

    首先我们知道,这个题可以N^2的做,我们先确定一个根,然后讨论下情况,合法的三个点只可能有三种情况,第一种是三个点有相同的lca,这种情况我们可以用tree-dp来解决,用dis[i][j]表示i为根 ...

  10. C# Selenium with PhantomJSDriver get image width and height (获取图片的长和高)

    //get image width and height var image=driver.FindElement(By.ClassName("it-Header_authorImage&q ...