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 ...
随机推荐
- eclipse创建android项目失败的问题 [ android support library ]
有根筋搭错了,想起来android应用开发???? 放下两年的手机应用开发,昨天有更新了android SDK, 重新搭建开发环境. 这两年android 变化真TM的大............... ...
- jsapi支付,提示redirect_uri 参数错误
检查授权目录(微信支付——配置中心) appid MCHID KEYS 配置参数是否正确 appsecrect 配置是否正确(开发者中心) 如果是使用测试链接,需要同时指定测试授权目录,测试账号,并且 ...
- (转)android Fragments详解三:实现Fragment的界面
为fragment添加用户界面 fragment一般作为activity的用户界面的一部分,把它自己的layout嵌入到activity的layout中. 一个 要为fragment提供layo ...
- Python实现C4.5(信息增益率)
Python实现C4.5(信息增益率) 运行环境 Pyhton3 treePlotter模块(画图所需,不画图可不必) matplotlib(如果使用上面的模块必须) 计算过程 st=>star ...
- Html5最简单的游戏Demo——Canvas绘图的弹弹球
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <t ...
- To add private variable to this Javascript literal object
You can use number as function/variable name, the numberic name can't be accessed from parent scope, ...
- nodeJs事件之监听移除事件
var EventEmitter=require('events').EventEmitter var life=new EventEmitter(); //comfort 求安慰,函数名: //fo ...
- android 自动化压力测试-monkey 1 实践
Monkey是Android中的一个命令行工具,可以运行在模拟器里或实际设备中.它向系统发送伪随机的用户事件流(如按键输入.触摸屏输入.手势输入等),实现对正在开发的应用程序进行压力测试.Monkey ...
- 用PHP向数据库中添加数据
显示页面(用户可见) <body><form action="chuli.php" method="post"> //将该页面接收的数 ...
- 设计模式之建造者模式(Builder)
建造者模式原理:建造模式主要是用于产生对象的各个组成部分,而抽象工厂模式则用于产生一系列对象,建造者模式而且要求这些对象的组成部分有序. 代码如下: #include <iostream> ...