Codeforces Round #419 (Div. 2) E. Karen and Supermarket(树形dp)
http://codeforces.com/contest/816/problem/E
题意:
去超市买东西,共有m块钱,每件商品有优惠卷可用,前提是xi商品的优惠券被用。问最多能买多少件商品?
思路:
第一件商品使用优惠券不需要前提,别的都是需要的,然后这样就形成了一棵以1为根的树。
这样,很容易想到是树形dp。
d【u】【j】【0/1】表示以u为根的子数中选择j件商品所需的最少花费,0/1表示u商品是否能用优惠券。
解释一下代码中的sz【】,它所代表的是以u为根的子树的结点数。

当我们现在访问的是1号结点时,sz【1】=1,然后访问2号结点,2号结点访问结束后,我们就会重新回到1号结点的dfs处,然后进行状态转移
for(int j=sz[u];j>=;j--) //表示sz【u】中选取的j个个数 for(int k=;k<=sz[v];k++) //表示v结点及其子树中选取的个数
最后将sz【2】的值加到sz【1】中,这样等下一次进行结点3的状态转移时,只需要考虑在2的子树中选取多少个和3的子树中选取多少个即可。到4时,只需要考虑在2和3中一共选取多少个和4的子树中选取多少个,因为前者在上一步已经计算出了最小花费。
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<sstream>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int,int> pll;
const int INF = 0x3f3f3f3f;
const int maxn = +; int n, m; int pa[maxn], pb[maxn];
int sz[maxn];
int d[maxn][maxn][]; vector<int> g[maxn]; void dfs(int u)
{
sz[u]=;
d[u][][]=, d[u][][]=pa[u], d[u][][]=pa[u]-pb[u];
for(int i=;i<g[u].size();i++)
{
int v=g[u][i];
dfs(v); for(int j=sz[u];j>=;j--)
{
for(int k=;k<=sz[v];k++)
{
d[u][j+k][]=min(d[u][j+k][],d[u][j][]+d[v][k][]);
d[u][j+k][]=min(d[u][j+k][],min(d[u][j][]+d[v][k][],d[u][j][]+d[v][k][]));
}
}
sz[u]+=sz[v];
}
} int main()
{
//freopen("in.txt","r",stdin);
while(~scanf("%d%d",&n, &m))
{
for(int i=;i<=n;i++) g[i].clear();
for(int i=;i<=n;i++)
{
scanf("%d%d",&pa[i],&pb[i]);
if(i>)
{
int x;
scanf("%d",&x);
g[x].push_back(i);
}
} memset(d,INF,sizeof(d));
dfs(); int i;
for(i=;i<=n;i++)
{
if(min(d[][i][],d[][i][])>m) break;
}
printf("%d\n",i-);
}
return ;
}
Codeforces Round #419 (Div. 2) E. Karen and Supermarket(树形dp)的更多相关文章
- 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 ...
- Codeforces Round #419 (Div. 2) B. Karen and Coffee(经典前缀和)
http://codeforces.com/contest/816/problem/B To stay woke and attentive during classes, Karen needs s ...
- Codeforces Round #419 (Div. 2) C. Karen and Game
C. Karen and Game time limit per test 2 seconds memory limit per test 512 megabytes input standard i ...
- Codeforces Round #419 (Div. 2) B. Karen and Coffee
To stay woke and attentive during classes, Karen needs some coffee! Karen, a coffee aficionado, want ...
- Codeforces Round #419 (Div. 2) A. Karen and Morning(模拟)
http://codeforces.com/contest/816/problem/A 题意: 给出一个时间,问最少过多少时间后是回文串. 思路: 模拟,先把小时的逆串计算出来: ① 如果逆串=分钟, ...
- 【找规律】【递推】【二项式定理】Codeforces Round #419 (Div. 1) B. Karen and Test
打个表出来看看,其实很明显. 推荐打这俩组 11 1 10 100 1000 10000 100000 1000000 10000000 100000000 1000000000 1000000000 ...
- 【贪心】 Codeforces Round #419 (Div. 1) A. Karen and Game
容易发现,删除的顺序不影响答案. 所以可以随便删. 如果行数大于列数,就先删列:否则先删行. #include<cstdio> #include<algorithm> usin ...
- Codeforces Round #196 (Div. 2) D. Book of Evil 树形dp
题目链接: http://codeforces.com/problemset/problem/337/D D. Book of Evil time limit per test2 secondsmem ...
- Codeforces Round #382 (Div. 2) 继续python作死 含树形DP
A - Ostap and Grasshopper zz题能不能跳到 每次只能跳K步 不能跳到# 问能不能T-G 随便跳跳就可以了 第一次居然跳越界0.0 傻子哦 WA1 n,k = map ...
随机推荐
- [MongoDB] 用户权限管理
在新环境按照原来的步骤新装了MongoDB,结果出现了一些错误,才发现版本升到了2.6.1,用户权限相关的内容全部改掉了. 现在使用Role来管理用户,有一些内置的Role,也可以自定义Role. 内 ...
- 【转载】web开发中 web 容器的作用(如tomcat)
我们讲到servlet可以理解服务器端处理数据的java小程序,那么谁来负责管理servlet呢?这时候我们就要用到web容器.它帮助我们管理着servlet等,使我们只需要将重心专注于业务逻辑. 什 ...
- 170712、springboot编程之集成shiro
这篇文章我们来学习如何使用Spring Boot集成Apache Shiro.安全应该是互联网公司的一道生命线,几乎任何的公司都会涉及到这方面的需求.在Java领域一般有Spring Security ...
- 详解spring boot mybatis全注解化
本文重点介绍spring boot mybatis 注解化的实例代码 1.pom.xml //引入mybatis <dependency> <groupId>org.mybat ...
- sqlserver如何读写操作windows系统的文件
DECLARE @object int DECLARE @hr int DECLARE @src varchar(255), @desc varchar ...
- ERROR 2002 (HY000): Can't connect to local MySQL server through socket
原文链接:https://blog.csdn.net/u011262253/article/details/82802157 一.错误现场还原: 下面我们通过三种方式来连接,然后观察提示的错误信息: ...
- MySQL 通用查询日志和慢查询日志分析
MySQL中的日志包括:错误日志.二进制日志.通用查询日志.慢查询日志等等.这里主要介绍下比较常用的两个功能:通用查询日志和慢查询日志. 1)通用查询日志:记录建立的客户端连接和执行的语句.2)慢查询 ...
- 第1章 1.8计算机网络概述--OSI参考模型和网络排错
OSI参考模型的网络排错: 每一层都为上一层提供服务. 如果网络出故障了,应该从底层向高层一层一层的查. OSI参考模型排错指导:(排错原则:自下而上.终极大招ping命令) 1.物理层故障: ①查看 ...
- andorid ListView和GirdView 与ScrollView 冲突
1.listview解决方法 public static void setListViewHeightBasedOnChildren(ListView listView) { if(listView ...
- 窗口-EasyUI Window 窗口、EasyUI Dialog 对话框、EasyUI Messager 消息框
EasyUI Window 窗口 扩展自 $.fn.panel.defaults.通过 $.fn.window.defaults 重写默认的 defaults. 窗口(window)是一个浮动的.可拖 ...