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. swagger2 坑 记录

    swagger2 只认 @RequestMapping 注解! 不认@RestController 注解 @RestController @RequestMapping(value = "/ ...

  2. Class.forName()与newInstance()

    import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sq ...

  3. replace()函数用法

    replace()函数表示将用一个字符串替换字符串中的所出现的特定内容. 语法为:replace(字段1,字段2,字段3),意思为字段3将会替换字段1里与字段2相同的内容  列如: table1 st ...

  4. [转载]Eclipse的常用快捷键

    常用的快捷键 ctrl+1:快速修复错误 ctrl+shift+L :查看快捷键 alt+?或alt+/:自动补全代码或者提示代码 ctrl+o:快速outline视图 ctrl+shift+r:打开 ...

  5. Iterator 和 for...of 循环

    Iterator(遍历器)的概念 § ⇧ JavaScript 原有的表示“集合”的数据结构,主要是数组(Array)和对象(Object),ES6 又添加了Map和Set.这样就有了四种数据集合,用 ...

  6. mysql数据库优化课程---14、常用的sql技巧

    mysql数据库优化课程---14.常用的sql技巧 一.总结 一句话总结:其实就是sql中那些函数的使用 1.mysql中函数如何使用? 选择字段 其实就是作用域select的选择字段 3.转大写: ...

  7. 设计模式--中介者模式C++实现

    中介者模式C++实现 1定义 用一个中介对象封装一系列的对象交互,中介者使各个对象不需要显示的相互作用,从而使其耦合松散,而且可以独立的改变他们之间的交互 2类图 组成说明 Mediator抽象中介者 ...

  8. 设计模式--代理模式C++实现

    代理模式C++实现 1定义 为其他对象提供一种代理以控制对这个对象的访问 2类图 角色定义: Subject抽象主体角色,抽象类或者接口,是一个普通的业务类型定义 RealSubject具体主体角色, ...

  9. 剑指offer算法总结

    剑指offer算法学习总结 节选剑指offer比较经典和巧妙的一些题目,以便复习使用.一部分题目给出了完整代码,一部分题目比较简单直接给出思路.但是不保证我说的思路都是正确的,个人对算法也不是特别在行 ...

  10. Tracing on Linux

    The Linux tracing APIs are a relatively new addition to the kernel and one of the most powerful new ...