HDU 4169 树形DP
Wealthy Family
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.
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.
0 10
1 15
1 25
1 35
4 45
3 3
0 10
1 10
2 10
impossible
//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的更多相关文章
- hdu 4123 树形DP+RMQ
http://acm.hdu.edu.cn/showproblem.php? pid=4123 Problem Description Bob wants to hold a race to enco ...
- HDU 1520 树形dp裸题
1.HDU 1520 Anniversary party 2.总结:第一道树形dp,有点纠结 题意:公司聚会,员工与直接上司不能同时来,求最大权值和 #include<iostream> ...
- HDU 1561 树形DP入门
The more, The Better Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- HDU 2196树形DP(2个方向)
HDU 2196 [题目链接]HDU 2196 [题目类型]树形DP(2个方向) &题意: 题意是求树中每个点到所有叶子节点的距离的最大值是多少. &题解: 2次dfs,先把子树的最大 ...
- HDU 1520 树形DP入门
HDU 1520 [题目链接]HDU 1520 [题目类型]树形DP &题意: 某公司要举办一次晚会,但是为了使得晚会的气氛更加活跃,每个参加晚会的人都不希望在晚会中见到他的直接上司,现在已知 ...
- codevs 1380/HDU 1520 树形dp
1380 没有上司的舞会 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 回到问题 题目描述 Description Ural大学有N个职员 ...
- HDU 5834 [树形dp]
/* 题意:n个点组成的树,点和边都有权值,当第一次访问某个点的时候获得利益为点的权值 每次经过一条边,丢失利益为边的权值.问从第i个点出发,获得的利益最大是多少. 输入: 测试样例组数T n n个数 ...
- hdu 4267 树形DP
思路:先dfs一下,找出1,n间的路径长度和价值,回溯时将该路径长度和价值清零.那么对剩下的图就可以直接树形dp求解了. #include<iostream> #include<al ...
- hdu 4607 (树形DP)
当时比赛的时候我们找出来只要求出树的最长的边的节点数ans,如果要访问点的个数n小于ans距离直接就是n-1 如果大于的话就是(n-ans)*2+ans-1,当时求树的直径难倒我们了,都不会树形dp ...
随机推荐
- poj 2631 Roads in the North
题目连接 http://poj.org/problem?id=2631 Roads in the North Description Building and maintaining roads am ...
- Android UmengShareSDK第三方登录
Android UmengShareSDK 第三方登录- 今天就不废话了,集成平台第三方登录.市面上集成平台有shareSDK 和 Ument两种,shareSDK的ipa和服务好些,如果自己研究会很 ...
- iOS关于打包出错
运行没问题,有可能是自动打包编译脚本的存在,删除掉即可.
- asp.net解决高并发的方案.
asp.net解决高并发的方案. Posted on 2012-11-27 22:31 75077027 阅读(3964) 评论(1) 编辑 收藏 最近几天一直在读代震军的博客,他是 Discuz!N ...
- Android实现SharePreferences和AutoCompletedTextView
Android实现SharePreferences和AutoCompletedTextView 开发工具:Andorid Studio 1.3 运行环境:Android 4.4 KitKat 工程内容 ...
- Json.Net使用JSON Schema验证JSON格式
Json.NET supports the JSON Schema standard via the JsonSchema and JsonValidatingReader classes. It s ...
- js 获取随机数
返回 m 到 n 的随机整数 <script type="text/javascript"> function randomNumber(m.n){ return Ma ...
- Chr()和chrb()的含义(转)
http://blog.csdn.net/cunxiyuan108/article/details/5989701 Chr(charcode) 必要的 charcode 参数是一个用来识别某字符的 L ...
- [shell基础]——find命令
find命令选项 -name 按照文件名查找 -type 查找某一类型的文件(b 代表设备块:d 目录:c 字符设备文件:l 符号(软)链接文件:f 普通文件) -size 查找文件长度或者大小 -p ...
- 软件工程随堂作业--随机产生30到四则运算(c语言)
#include "stdio.h" #include "math.h" #include "stdlib.h" #include" ...