Problem Description
While studying the history of royal families, you want to know how wealthy each family is. While you have various 'net worth' figures for each individual throughout history, this is complicated by double counting caused by inheritance. One way to estimate the wealth of a family is to sum up the net worth of a set of k people such that no one in the set is an ancestor of another in the set. The wealth of the family is the maximum sum achievable over all such sets of k people. Since historical records contain only the net worth of male family members, the family tree is a simple tree in which every male has exactly one father and a non-negative number of sons. You may assume that there is one person who is an ancestor of all other family members.
 
Input
The input consists of a number of cases. Each case starts with a line containing two integers separated by a space: N (1 <= N <= 150,000), the number of people in the family, and k (1 <= k <= 300), the size of the set. The next N lines contain two non-negative integers separated by a space: the parent number and the net worth of person i (1 <= i <= N). Each person is identified by a number between 1 and N, inclusive. There is exactly one person who has no parent in the historical records, and this will be indicated with a parent number of 0. The net worths are given in millions and each family member has a net worth of at least 1 million and at most 1 billion.
 
Output
For each case, print the maximum sum (in millions) achievable over all sets of k people satisfying the constraints given above. If it is impossible to choose a set of k people without violating the constraints, print 'impossible' instead.
 
题目大意:给n个点,每个点有一个权值,要求选k个点,这k个点任意一个点不能为其他点的父节点,求最大总权值。
思路:树形DP。ex[i]代表从开始遍历到某点x及其子节点(包括x)选i个点可以得到的最大总权值,now[i]代表从开始遍历到某点x及其子节点(不包括x)选i个点可以得到的最大总权值,函数一开始的时候ex是代表遍历到x之前的选i个点可以得到的最大总权值,那么在dfs算出now之后,ex[i] = max(now[i], ex[i-1] + a)就可以保证没有选的两个点是不符合要求的。
 
 #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)的更多相关文章

  1. POJ 2342 &&HDU 1520 Anniversary party 树形DP 水题

    一个公司的职员是分级制度的,所有员工刚好是一个树形结构,现在公司要举办一个聚会,邀请部分职员来参加. 要求: 1.为了聚会有趣,若邀请了一个职员,则该职员的直接上级(即父节点)和直接下级(即儿子节点) ...

  2. hdu 5452 Minimum Cut 树形dp

    Minimum Cut Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=54 ...

  3. HDU 1520 Anniversary party [树形DP]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1520 题目大意:给出n个带权点,他们的关系可以构成一棵树,问从中选出若干个不相邻的点可能得到的最大值为 ...

  4. hdu 1520Anniversary party(简单树形dp)

    Anniversary party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  5. Install Air Conditioning HDU - 4756(最小生成树+树形dp)

    Install Air Conditioning HDU - 4756 题意是要让n-1间宿舍和发电站相连 也就是连通嘛 最小生成树板子一套 但是还有个限制条件 就是其中有两个宿舍是不能连着的 要求所 ...

  6. HDU 3586 二分答案+树形DP判定

    HDU 3586 『Link』HDU 3586 『Type』二分答案+树形DP判定 ✡Problem: 给定n个敌方据点,1为司令部,其他点各有一条边相连构成一棵树,每条边都有一个权值cost表示破坏 ...

  7. HDU 3586.Information Disturbing 树形dp 叶子和根不联通的最小代价

    Information Disturbing Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/ ...

  8. HDU 5293 Annoying problem 树形dp dfs序 树状数组 lca

    Annoying problem 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 Description Coco has a tree, w ...

  9. HDU - 3586 Information Disturbing 树形dp二分答案

    HDU - 3586 Information Disturbing 题目大意:从敌人司令部(1号节点)到前线(叶子节点)的通信路径是一个树形结构,切断每条边的联系都需要花费w权值,现在需要你切断前线和 ...

随机推荐

  1. swift3.0 保存图片到本地,申请权限

    1.info中写上 <key>NSCameraUsageDescription</key> <string>需要您的同意才能读取媒体资料库</string&g ...

  2. SSM+poi导入和导出

    最原始数据 导入成功后 下载数据 下载后的数据显示 数据变成16条 点击导出可选择 导了两次  看数据变化 数据库字段在下面地址给出 首先贴出Dao层 List<User> findAll ...

  3. npm 取消代理 npm config delete proxy

    今天在安装electron时设置了代理,发现再npm install 安装别的总是装不上,只好取消代理. npm 取消代理 npm config delete proxy

  4. CRM2Stark组件

    CRM stark组件 开启新项目:CRM 再创建一个应用app02(python manage.py startapp app02 或者是工具栏:run...task:执行startapp app0 ...

  5. Python学习笔记三:数据类型

    数据类型 整数int 32位机器,-2**31~2**31-1,即-2147483648~2147483647(4亿多) 64位机器,-2**63~2**63-1,非常大了. 长整型long 没有位数 ...

  6. arping命令用法

    arping命令使用说明 BusyBox v1.17.3 (2011-07-20 17:01:30 CST) multi-call binary. Usage: arping [-fqbDUA] [- ...

  7. C++ 数组复制

    参考:https://blog.csdn.net/huangxiaohui123/article/details/81984175 补充: int tu[N][N],dis[N][N]; memcpy ...

  8. 动态代理以及对应Spring中AOP源码分析

    AOP(面向切面编程)在Spring中是被广泛应用的(例如日志,事务,权限等),而它的基本原理便是动态代理. 我们知道动态代理有两种:基于JDK的动态代理以及基于CGlib动态代理.以下是两种动态代理 ...

  9. 成都Uber优步司机奖励政策(1月7日)

    1月7日 奖励政策 滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblog ...

  10. iOS WKWebView添加进度条02

    之前写了一个是关于webview添加进度条的,现在补一个WKWebView进度条. //添加一个全局属性 @property(nonatomic,strong)CALayer *progresslay ...