LINK


思路

首先发现依赖关系是一个树形的结构

然后因为直接算花多少钱来统计贡献不是很好

因为数组开不下

那就可以算一个子树里面选多少个的最小代价就可以了

注意统计贡献的时候用优惠券的答案只能在1号点进行统计


//Author: dream_maker
#include<bits/stdc++.h>
using namespace std;
//----------------------------------------------
//typename
typedef long long ll;
//convenient for
#define fu(a, b, c) for (int a = b; a <= c; ++a)
#define fd(a, b, c) for (int a = b; a >= c; --a)
#define fv(a, b) for (int a = 0; a < (signed)b.size(); ++a)
//inf of different typename
const int INF_of_int = 1e9;
const ll INF_of_ll = 1e18;
//fast read and write
template <typename T>
void Read(T &x) {
bool w = 1;x = 0;
char c = getchar();
while (!isdigit(c) && c != '-') c = getchar();
if (c == '-') w = 0, c = getchar();
while (isdigit(c)) {
x = (x<<1) + (x<<3) + c -'0';
c = getchar();
}
if (!w) x = -x;
}
template <typename T>
void Write(T x) {
if (x < 0) {
putchar('-');
x = -x;
}
if (x > 9) Write(x / 10);
putchar(x % 10 + '0');
}
//----------------------------------------------
const int N = 5010;
struct Edge{
int v, nxt;
}E[N << 1];
int head[N], tot = 0;
ll c[N], d[N], siz[N], n;
ll dp[N][N][2], ans = 0, B;
void add(int u, int v) {
E[++tot] = (Edge){v, head[u]};
head[u] = tot;
}
void dfs(int u, int fa) {
siz[u] = 1;
dp[u][1][1] = c[u] - d[u];
dp[u][1][0] = c[u];
for (int i = head[u]; i; i = E[i].nxt) {
int v = E[i].v;
if (v == fa) continue;
dfs(v, u);
fd(j, siz[u], 0)
fd(k, siz[v], 0) {
dp[u][j + k][1] = min(dp[u][j + k][1], dp[u][j][1] + min(dp[v][k][0], dp[v][k][1]));
dp[u][j + k][0] = min(dp[u][j + k][0], dp[u][j][0] + dp[v][k][0]);
}
siz[u] += siz[v];
}
fu(i, ans + 1, siz[u]) {
if (dp[u][i][0] <= B) ans = i;
else break;
}
}
int main() {
memset(dp, 0x3f, sizeof(dp));
Read(n); Read(B);
fu(i, 1, n) {
Read(c[i]); Read(d[i]);
dp[i][0][0] = 0;
if (i > 1) {
int u; Read(u);
add(i, u);
add(u, i);
}
}
dfs(1, 0);
fu(i, ans + 1, n) if (dp[1][i][1] <= B) ans = i;
Write(ans);
return 0;
}

Codeforces 815C. 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. CF815C Karen and Supermarket [树形DP]

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

  4. 816E. Karen and Supermarket 树形DP

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

  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 816E Karen and Supermarket ——(树形DP)

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

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

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

  8. codeforces 161D Distance in Tree 树形dp

    题目链接: http://codeforces.com/contest/161/problem/D D. Distance in Tree time limit per test 3 secondsm ...

  9. codeforces 337D Book of Evil (树形dp)

    题目链接:http://codeforces.com/problemset/problem/337/D 参考博客:http://www.cnblogs.com/chanme/p/3265913 题目大 ...

随机推荐

  1. [异常记录(三)] 从 bcp 客户端收到一个对 colid 12 无效的列长度

    这个问题是使用SqlBulkCopy拷贝数据,字符串长度超出数据类型长度导致的. 处理过程中对长度进行判断并截取就OK了. *注:SqlBulkCopy 这货 要求ColumnMappings 列的大 ...

  2. [小问题笔记(四)] Enum枚举类型转换为DataTable( C# )

    枚举: public enum ProductType { 小产品=, 大产品, 超大产品 } 转换方法: /// <summary> /// 枚举类型转化为DataTable /// & ...

  3. centos mysql忘记密码

    1.停止mysql 服务:service mysqld stop; 2.vim命令打开mysql配置文件my.cnf(位置一般为:/etc/my.cnf) 3.在mysqld进程配置文件中添加skip ...

  4. AtCoder Grand Round 012B Splatter Painting

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  5. vue-cli background iamge

    vue-cli 可以将图片直接放在项目生成的 static 文件夹里,然后在components里面直接采用绝对路径去取就可以了. 在根目录里面都会有一个static目录,这个是用来存放静态文件的,把 ...

  6. PHP处理MySQL事务代码

    php使用mysqli进行事务处理 <?php$db = new mysqli("localhost","root",""," ...

  7. js原码工具集

    /* 原生js工具集 */ arr是否包含obj function contains1(arr, obj) { var i = arr.length; while (i--) { if (arr[i] ...

  8. bootstrap系统学习

    1.响应式中注意的内容: 一行(row)必须在.container中. col-xs- col-sm- col-md- col-lg- 列偏移 .col-md-offset-* 列排序 .col-md ...

  9. POJ-3659-最小支配集裸题/树形dp

    Cell Phone Network Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7127   Accepted: 254 ...

  10. Im4java+ImageMagick/GraphicsMagick

    im4java的jar包可以在maven库中找到. <dependency> <groupId>org.im4java</groupId> <artifact ...