# Codeforces 815 C 树形依赖背包 解题报告

xiaoxiao2021-02-28  14

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

【解题报告】 题意： 商店里有n个物品，第i个物品的价格为ci元。每个物品只能买一次。商店发行了n张优惠券，每个物品各有一张优惠卷。如果使用了第i张优惠券，可以使该物品便宜di元钱，必须买商品才能够使用相对应的优惠券。第1张优惠卷可以无条件使用，但对于第i>=2张优惠卷，如果需要使用第i张优惠券，则必须先使用xi这张优惠券。 现在有b元钱，问最多能购买多少商品。

#include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define N 5010 #define inf 0x3f3f3f3f int cnt=-1,head[N]; struct Edge{int to,nxt;}e[N<<1]; int n,b,ans; int c[N],d[N],w[N]; int dp[N][N][2],size[N]; void adde(int u,int v) { e[++cnt].to=v; e[cnt].nxt=head[u]; head[u]=cnt; } void dfs(int u) { size[u]=1;dp[u][0][0]=0; dp[u][1][0]=c[u];dp[u][1][1]=c[u]-d[u]; for(int i=head[u];~i;i=e[i].nxt) { int v=e[i].to;dfs(v); for(int j=size[u];j>=0;j--) for(int k=0;k<=size[v];k++) { dp[u][j+k][0]=min(dp[u][j+k][0],dp[u][j][0]+dp[v][k][0]); dp[u][j+k][1]=min(dp[u][j+k][1],dp[u][j][1]+dp[v][k][1]); dp[u][j+k][1]=min(dp[u][j+k][1],dp[u][j][1]+dp[v][k][0]); } size[u]+=size[v]; } } int main() { freopen("shopping.in","r",stdin); freopen("shopping.out","w",stdout); memset(head,-1,sizeof(head)); memset(dp,inf,sizeof(dp)); scanf("%d%d",&n,&b); scanf("%d%d",&c[1],&d[1]); for(int i=2;i<=n;i++) { int u;scanf("%d%d%d",&c[i],&d[i],&u); adde(u,i); } dfs(1);ans=n; while(dp[1][ans][0]>b&&dp[1][ans][1]>b) ans--; printf("%d",ans); return 0; }