Wealthy Family

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.
 
Sample Input
5 3
0 10
1 15
1 25
1 35
4 45
3 3
0 10
1 10
2 10
 
Sample Output
85
impossible
题意:
   从一棵树的N <= 150000个结点中选出K个使得权值和最大, 选出的K个点两两之间都不是祖先关系
题解:我都写了些什么鬼,以后再看吧
 
//meek///#include<bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <sstream>
#include <vector>
using namespace std ;
#define mem(a) memset(a,0,sizeof(a))
#define pb push_back
#define fi first
#define se second
#define MP make_pair
typedef long long ll; const int N = ;
const int inf = ;
const int mod= ; int dp[N],root,n,k,a[N];//k
vector<int >G[N];
void dfs(int x,int *dp) {
int last[N];
for(int i=;i<=k;i++) last[i]=dp[i];
for(int i=;i<G[x].size();i++) {
int v=G[x][i];
dfs(v,last);
}
for(int i=k;i>=;i--) {
if(dp[i-]||!(i-)) {
dp[i]=max(last[i],dp[i-]+a[x]);
}
else dp[i]=last[i];
}
}
int main() {
int u;
while(scanf("%d%d",&n,&k)!=EOF) {
for(int i=;i<=n;i++) G[i].clear();
for(int i=;i<=n;i++) {
scanf("%d%d",&u,&a[i]);
if(u==) root=i;
else G[u].pb(i);
}
for(int i=;i<=k;i++) dp[i]=;
dfs(root,dp);
if(dp[k]) {
cout<<dp[k]<<endl;
}
else printf("impossible\n");
}
return ;
}

HDU 4169 树形DP的更多相关文章

  1. hdu 4123 树形DP+RMQ

    http://acm.hdu.edu.cn/showproblem.php? pid=4123 Problem Description Bob wants to hold a race to enco ...

  2. HDU 1520 树形dp裸题

    1.HDU 1520  Anniversary party 2.总结:第一道树形dp,有点纠结 题意:公司聚会,员工与直接上司不能同时来,求最大权值和 #include<iostream> ...

  3. HDU 1561 树形DP入门

    The more, The Better Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  4. HDU 2196树形DP(2个方向)

    HDU 2196 [题目链接]HDU 2196 [题目类型]树形DP(2个方向) &题意: 题意是求树中每个点到所有叶子节点的距离的最大值是多少. &题解: 2次dfs,先把子树的最大 ...

  5. HDU 1520 树形DP入门

    HDU 1520 [题目链接]HDU 1520 [题目类型]树形DP &题意: 某公司要举办一次晚会,但是为了使得晚会的气氛更加活跃,每个参加晚会的人都不希望在晚会中见到他的直接上司,现在已知 ...

  6. codevs 1380/HDU 1520 树形dp

    1380 没有上司的舞会 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 回到问题 题目描述 Description Ural大学有N个职员 ...

  7. HDU 5834 [树形dp]

    /* 题意:n个点组成的树,点和边都有权值,当第一次访问某个点的时候获得利益为点的权值 每次经过一条边,丢失利益为边的权值.问从第i个点出发,获得的利益最大是多少. 输入: 测试样例组数T n n个数 ...

  8. hdu 4267 树形DP

    思路:先dfs一下,找出1,n间的路径长度和价值,回溯时将该路径长度和价值清零.那么对剩下的图就可以直接树形dp求解了. #include<iostream> #include<al ...

  9. hdu 4607 (树形DP)

    当时比赛的时候我们找出来只要求出树的最长的边的节点数ans,如果要访问点的个数n小于ans距离直接就是n-1 如果大于的话就是(n-ans)*2+ans-1,当时求树的直径难倒我们了,都不会树形dp ...

随机推荐

  1. notepad++ erlang开发环境设置

    初学erlang 网上有使用eclipse的,有使用emacs的,尝试了一下, 感觉太麻烦,来试试notepad++吧. 有什么新使用方法会再更新上来,for you for me. 1.语法高亮: ...

  2. b75,gtx560,I5 安装10.10.2

    1.安装变色龙,wowpc.iso,这个是可以让电脑从windows引导 mac 安装的. 2.把黑苹果CDR压到一个硬盘分区里去. 3.安装10.10.2,把安装盘里的extra拷贝到 系统盘里 , ...

  3. iOS 初级数据持久化

    数据持久化 什么是数据持久化? 数据的永久存储 为什么要做数据持久化::存储在内存中的数据,程序关闭,内存释放,数据丢失,这种数据是临时的 数据持久化的本质:数据保存成文件,存储到程序的沙盒中 一.沙 ...

  4. [备忘]Asp.net MVC 将服务端Model传递的对象转为客户端javascript对象

    <script type="text/javascript"> var jsObject = @Html.Raw(Json.Encode(Model.Objects)) ...

  5. EF6 在原有数据库中使用 CodeFirst 总复习(三、重建迁移)

    本来原来学的时候,挺顺利的,没想到再次使用,还是遇到很多问题,导致更新失败,所以,只能重建迁移,免得看着乱乱的. 一.删除迁移,将数据恢复到(一)结束状态 1.删除文件夹 2.删除表 3.删除列 4. ...

  6. bootstrap-fileinput 图片上传

    bootstrap-fileinput 源文件 在网上下载 CSS: <link href="../../static/Bootstrap/css/plugins/bootstrap- ...

  7. 安装MSITVPN连接的时候弹出:需要(未知)上的文件'MSITVPN.bmp。

    使用 msitvpn 连接microsoft 公司内网,在安装msitvpn的时候突然弹出一个对话框提示需要msitvpn.bmp 文件,找了很久都没找到解决问题方案. 最后只能猜测是不是和用户的权限 ...

  8. 41.使用Chipscope时如何防止reg_wire型信号被优化掉

    随着FPGA设计复杂程度越来越高,芯片内部逻辑分析功能显得越来越重要.硬件层次上的逻辑分析仪价格十分昂贵,而且操作比较复杂.目前,FPGA芯片的两大供应商都为自己的FPGA芯片提供了软件层面上的逻辑分 ...

  9. oracle增加表空间的四种方法,查询表空间使用情况

    增加表空间大小的四种方法Meathod1:给表空间增加数据文件ALTER TABLESPACE app_data ADD DATAFILE'D:\ORACLE\PRODUCT\10.2.0\ORADA ...

  10. 团队项目NABC分析

    我们的团队项目是“来用”实用工具集合软件,我针对我们项目功能丰富的特点进行NABC分析. N (Need 需求):我们的软件正是从最大程度上满足用户需求出发,因为软件集合了不同种类的功能,可以满足用户 ...