CF816E / CF815C《Karen and Supermarket》
Description
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 dollars.
The supermarket sells goods. The -th good can be bought for 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 coupons. If Karen purchases the -th good, she can use the -th coupon to decrease its price by . Of course, a coupon cannot be used without buying the corresponding good.
There is, however, a constraint with the coupons. For all , in order to use the -th coupon, Karen must also use the -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 ?
Input Format
The first line of input contains two integers nn and bb ( 1<=n<=50001<=n<=5000 , 1<=b<=10^{9}1<=b<=109 ), the number of goods in the store and the amount of money Karen has, respectively.
The next nn lines describe the items. Specifically:
- The ii -th line among these starts with two integers, c_{i}ci and d_{i}di ( 1<=d_{i}<c_{i}<=10^{9}1<=di<ci<=109 ), the price of the ii -th good and the discount when using the coupon for the ii -th good, respectively.
- If i>=2i>=2 , this is followed by another integer, x_{i}xi ( 1<=x_{i}<i1<=xi<i ), denoting that the x_{i}xi -th coupon must also be used before this coupon can be used.
Output Format
Output a single integer on a line by itself, the number of different goods Karen can buy, without exceeding her budget.
Input / Output Samples
Input #1
1 |
|
Output #1
1 |
|
Input #2
1 |
|
Output #2
1 |
|
解析
从代码里复制过来的(
一道树形 DP
显然优惠券的使用逻辑可以构成一棵树
设 表示在树根为 i 的子树中购买 j 件商品,是(1)否(0)可以使用优惠券的最小花费
初始状态:
转移方程:
其中 ,i 需要倒序枚举来避免重复选商品
代码实现
1 |
|
- 本文作者:Handwer STD
- 本文链接:https://blog.handwer-std.top/2019-07-30/CF816E-CF815C/index.html
- 版权声明:本博客所有文章均采用 BY-NC-SA 许可协议,转载请注明出处!