HDU 4169 Wealthy Family(树形DP)
#include <cstdio>
#include <cstring> const int MAXN = ; int head[MAXN], next[MAXN], to[MAXN];
int a[MAXN];
int ecnt, n, k;
int ans[]; inline void addEdge(int u, int v) {
to[ecnt] = v;
next[ecnt] = head[u]; head[u] = ecnt++;
//printf("%d->%d\n",u,v);
} inline void init() {
memset(head, , sizeof(head));
memset(ans, , sizeof(ans));
ecnt = ;
int f;
for(int i = ; i <= n; ++i) {
scanf("%d%d", &f, &a[i]);
addEdge(f, i);
}
} inline void _max(int &a, const int &b) {
if(a < b) a = b;
} void dfs(int u, int *ex) {
int *now = new int[];
for(int i = ; i <= k; ++i) now[i] = ex[i];
for(int p = head[u]; p; p = next[p]) {
dfs(to[p], now);
}
ex[] = ;
for(int i = k; i > ; --i) {
ex[i] = now[i];
if(ex[i-] != -) {
_max(ex[i], ex[i-] + a[u]);
}
}
delete [] now;
} int main() {
while(scanf("%d%d", &n, &k) != EOF) {
init();
dfs(,ans);
if(ans[k] == -) puts("impossible");
else printf("%d\n", ans[k]);
}
return ;
}
HDU 4169 Wealthy Family(树形DP)的更多相关文章
- POJ 2342 &&HDU 1520 Anniversary party 树形DP 水题
一个公司的职员是分级制度的,所有员工刚好是一个树形结构,现在公司要举办一个聚会,邀请部分职员来参加. 要求: 1.为了聚会有趣,若邀请了一个职员,则该职员的直接上级(即父节点)和直接下级(即儿子节点) ...
- hdu 5452 Minimum Cut 树形dp
Minimum Cut Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=54 ...
- HDU 1520 Anniversary party [树形DP]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1520 题目大意:给出n个带权点,他们的关系可以构成一棵树,问从中选出若干个不相邻的点可能得到的最大值为 ...
- hdu 1520Anniversary party(简单树形dp)
Anniversary party Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- Install Air Conditioning HDU - 4756(最小生成树+树形dp)
Install Air Conditioning HDU - 4756 题意是要让n-1间宿舍和发电站相连 也就是连通嘛 最小生成树板子一套 但是还有个限制条件 就是其中有两个宿舍是不能连着的 要求所 ...
- HDU 3586 二分答案+树形DP判定
HDU 3586 『Link』HDU 3586 『Type』二分答案+树形DP判定 ✡Problem: 给定n个敌方据点,1为司令部,其他点各有一条边相连构成一棵树,每条边都有一个权值cost表示破坏 ...
- HDU 3586.Information Disturbing 树形dp 叶子和根不联通的最小代价
Information Disturbing Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/ ...
- HDU 5293 Annoying problem 树形dp dfs序 树状数组 lca
Annoying problem 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 Description Coco has a tree, w ...
- HDU - 3586 Information Disturbing 树形dp二分答案
HDU - 3586 Information Disturbing 题目大意:从敌人司令部(1号节点)到前线(叶子节点)的通信路径是一个树形结构,切断每条边的联系都需要花费w权值,现在需要你切断前线和 ...
随机推荐
- swift3.0 保存图片到本地,申请权限
1.info中写上 <key>NSCameraUsageDescription</key> <string>需要您的同意才能读取媒体资料库</string&g ...
- SSM+poi导入和导出
最原始数据 导入成功后 下载数据 下载后的数据显示 数据变成16条 点击导出可选择 导了两次 看数据变化 数据库字段在下面地址给出 首先贴出Dao层 List<User> findAll ...
- npm 取消代理 npm config delete proxy
今天在安装electron时设置了代理,发现再npm install 安装别的总是装不上,只好取消代理. npm 取消代理 npm config delete proxy
- CRM2Stark组件
CRM stark组件 开启新项目:CRM 再创建一个应用app02(python manage.py startapp app02 或者是工具栏:run...task:执行startapp app0 ...
- Python学习笔记三:数据类型
数据类型 整数int 32位机器,-2**31~2**31-1,即-2147483648~2147483647(4亿多) 64位机器,-2**63~2**63-1,非常大了. 长整型long 没有位数 ...
- arping命令用法
arping命令使用说明 BusyBox v1.17.3 (2011-07-20 17:01:30 CST) multi-call binary. Usage: arping [-fqbDUA] [- ...
- C++ 数组复制
参考:https://blog.csdn.net/huangxiaohui123/article/details/81984175 补充: int tu[N][N],dis[N][N]; memcpy ...
- 动态代理以及对应Spring中AOP源码分析
AOP(面向切面编程)在Spring中是被广泛应用的(例如日志,事务,权限等),而它的基本原理便是动态代理. 我们知道动态代理有两种:基于JDK的动态代理以及基于CGlib动态代理.以下是两种动态代理 ...
- 成都Uber优步司机奖励政策(1月7日)
1月7日 奖励政策 滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblog ...
- iOS WKWebView添加进度条02
之前写了一个是关于webview添加进度条的,现在补一个WKWebView进度条. //添加一个全局属性 @property(nonatomic,strong)CALayer *progresslay ...