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

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
6 16
10 9
10 5 1
12 2 1
20 18 3
10 2 3
2 1 5
output
4
input
5 10
3 1
3 1 1
3 1 2
3 1 3
3 1 4
output
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,一开始觉得就是做n次分组背包,但是发现是O(n^3)。

其实统计一下当前根节点所计算过的节点数,保存在size中就优化成O(n^2)了。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
using namespace std;
int n,m;
struct node
{
int next,to;
} edge[];
int head[],sze=;
void putin(int from,int to)
{
sze++;
edge[sze].next=head[from];
edge[sze].to=to;
head[from]=sze;
}
long long f[][][],size[],a[],b[];
void dfs(int u)
{
int i;
long long j,l;
size[u]=;
f[u][][]=;
f[u][][]=a[u];
f[u][][]=a[u]-b[u];
for(i=head[u]; i!=-; i=edge[i].next)
{
int y=edge[i].to;
dfs(y);
for(j=size[u]; j>=; j--)
{
for(l=; l<=size[y]; l++)
{
f[u][j+l][]=min(f[u][j+l][],f[u][j][]+f[y][l][]);
f[u][j+l][]=min(f[u][j+l][],min(f[u][j][]+f[y][l][],f[u][j][]+f[y][l][]));
}
}
size[u]+=size[y];
}
}
int main()
{
int i,j,fa;
memset(head,-,sizeof(head));
scanf("%d%d",&n,&m);
scanf("%I64d%I64d",&a[],&b[]);
for(i=; i<=n; i++)
{
scanf("%I64d%I64d%d",&a[i],&b[i],&fa);
putin(fa,i);
}
memset(f,/,sizeof(f));
dfs();
int ans=;
for(i=; i<=n; i++)
{
if(f[][i][]<=m||f[][i][]<=m)ans=i;
}
printf("%d\n",ans);
return ;
}

E. Karen and Supermarket的更多相关文章

  1. Codeforces 815C Karen and Supermarket 树形dp

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

  2. CF815C Karen and Supermarket

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

  3. CF815C Karen and Supermarket [树形DP]

    题目传送门 Karen and Supermarket On the way home, Karen decided to stop by the supermarket to buy some gr ...

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

  5. codeforces 815C Karen and Supermarket

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

  6. codeforces round #419 E. Karen and Supermarket

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

  7. Codeforces 815 C Karen and Supermarket

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

  8. 【Codeforces 815C】Karen and Supermarket

    Codeforces 815 C 考虑树型dp. \(dp[i][0/1][k]\)表示现在在第i个节点, 父亲节点有没有选用优惠, 这个子树中买k个节点所需要花的最小代价. 然后转移的时候枚举i的一 ...

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

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

随机推荐

  1. 洛谷 P5061 秘密任务 —— 二分图

    题目:https://www.luogu.org/problemnew/show/P5061 首先,“配合默契”就是连边的意思: 但发现答案不好统计,因为有连边的两个点可以分在一组,也可以不分在一组: ...

  2. MVC之一、预备知识储备

    自动属性 隐式类型 对象初始化器与集合初始化器 匿名类 扩展方法 Lambda表达式 (1).自动属性(Auto-Implemented Properties) C#自动属性可以避免原来这样我们手工声 ...

  3. AndroidStudio启动时不自动打开项目

    取消勾选Reopen last project on startup选项 点击 OK 就行了

  4. AI-Info-Micron-Insight:Intelligence Accelerated™

    ylbtech-AI-Info-Micron-Insight:Intelligence Accelerated™ Intelligence Accelerated™ Micron Insight 大会 ...

  5. MySQL 学习三 关于转义

    DB2 LIKE谓词查询语句中支持 百分号(%).下划线(_)的使用,不支持方括号([])(注:它会把方括号当成实际的值而非通配符),当我们需要在LIKE 查询条件中将百分号(%).下划线(_)作为实 ...

  6. SRAM SROM DRAM DROM DDR NAND FLASH EMMC的区别

    RAM(Random Access Memory)的全名为随机存取记忆体,它相当于PC机上的移动存储,用来存储和保存数据的.它在任何 时候都可以读写,RAM通常是作为操作系统或其他正在运行程序的临时存 ...

  7. ubuntu下终于安装好了nvidia的gt540显卡驱动

    ubuntu下终于安装好了nvidia的gt540显卡驱动.估计好多童鞋怕麻烦都放弃安装了哈. 先看看效果. ~$ lspci |grep -i vga :) :00.0 VGA compatible ...

  8. state estimation for robotics-1

    概率论是探讨SLAM的一个重要的工具,概率密度函数的概率意义在于它能够描述一个随机变量位于任意区间的概率. p(x<=x<=x+dx)≍p(x).dx(由拉格朗日中值定理)

  9. nginx是如何处理一个请求的(包含https配置)

    配置https首先要有ssl证书,这个证书目前阿里有免费的,但如果自己做实验,也是可以自签证书,只不过不受信 openssl genrsa -des3 -out server.key 1024     ...

  10. 使用Xilinx SDSoc在Xilinx zcu102开发板上编程HelloWorld

    关于Xilinx SDSoc的介绍我就不再复述了,我理解的也不一定准确,可以阅读官方文档了解SDSoc,你可以把它理解为一个集成开发环境 (IDE),通过SDSoc我们能够简单快速的对Xilinx的开 ...