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. Matlab 编程入门(一):编程基础

    上学期学了一些matlab的知识,这学期再用时竟然发现已经忘得差不多了(┬_┬) 于是决定重新开始并将它们记录下来,也方便自己以后查漏补缺! M文件编程 脚本文件 matlab有自己的命令行窗口,对于 ...

  2. detach()之大坑:detach会引起局部变量失效引起线程对内存的非法访问题。

    detach()之大坑:detach会引起局部变量失效引起线程对内存的非法访问题.一:传递临时对象作为线程参数(1.1)要避免的陷阱(解释一)(1.2)要避免的陷阱(解释一)事实一:只要用临时构造的A ...

  3. redis参数AOF参数的bug

    问题:redis 不想开启AOF了.但是还老是出现BGREWRITEAOF .(本redis版本为4.0.6) 涉及持久化参数设置如下: 排查及结果: 该redis以前开启过 AOF ,后来停止AOF ...

  4. WebService 学习笔记(一、概念及定义)

    定义 WebService是一种服务导向架构(SOA service-oriented architecture)的技术,通过标准的Web协议提供服务,目的是保证不同平台的应用服务可以互操作. Web ...

  5. HTML中汉字空格占位符

    == 普通的英文半角空格   ==   ==   == no-break space (普通的英文半角空格但不换行)   == 中文全角空格 (一个中文宽度)   ==   == en空格 (半个中文 ...

  6. Python入门(案例)

    Python入门(案例) #一.上课案例: #输出hello wordprint('hello word') #python注释有两种#1.单行注释#这是单行注释#2.多行注释'''这是多行注释''' ...

  7. 【laravel】同一代码段内,先更新数据,后查询修改的数据,查询结果错误的问题

    如标题所言,是什么意思呢?举个栗子,需求如下: 你是一个电话销售人员,手头有一些待call电话单,每个电话单上有N个不同的电话号码,需要你每打一个电话就标记为”已打“.当一个电话单上的号码都标记为”已 ...

  8. python的基本知识

    1. python的简介    python的创始⼈人为吉多·范罗苏姆(Guido van Rossum).1989年年的圣诞节期间,吉多· 范罗苏姆为了了在阿姆斯特丹丹打发时间,决⼼心开发⼀个新的脚 ...

  9. reids同步机制和远程连接

    RDB同步机制: 开启和关闭:默认情况下是开启了.如果想关闭,那么注释掉redis.conf文件中的所有save选项就可以了. 同步机制: save 900 1:如果在900s以内发生了1次数据更新操 ...

  10. 【8086汇编-Day5】第二次实验

    debug的使用 偷个懒,之前写过了这里不再赘述 实验 1)实验1 要求:用e将一些数据写入内存,用a写入一段程序,t 逐条执行 观察具体参数变化,并探究现象 1.e写入,d检查 2.a写入程序 3. ...