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 ...
随机推荐
- iOS-动态调整UITableViewCell的高度
OS-动态调整UITableViewCell的高度iOS开发文档, by 友盟翻译组 stefaliu. 大概你第一眼看来,动态调整高度是一件不容易的事情,而且打算解决它的第一个想法往往是不正确的.在 ...
- StatusBar & StatusBarItem
StatusBar & StatusBarItem StatusBar StatusBar class StatusBarItem StatusBarItem class Example &l ...
- Entity Framework学习笔记(三)----CRUD(2)
请注明转载地址:http://www.cnblogs.com/arhat 昨天晚上老魏配的机器终于到了,可是拿回来之后什么都组装好了,唯独差一个非常重要的组件"电源线",老魏那个汗 ...
- 触发器(trigger)的作用???
1.触发器,英文名trigger,可以简单的理解为: 就相当于是一个事件的触发装置,当满足了一定的事件触发条件后进行相应的操作 例如当复位set信号到来时,我们就让A<=B,这样一个系统就是一个 ...
- 使用C语言在Win控制台中输出带颜色的文字
学了这么久的C语言,一直停留在编写“控制台”程序的水平.黑色窗口,白色的文字,看多了着实让人感到枯燥无味.但是作为业余爱好者,我既没有那么多时间和精力去学习如何编写窗口程序,也没有那个必要一定用C去调 ...
- 团队项目(NABC分析)
我们团队开发的是<校园导航>软件 (1)N(Need需求) 我们的团队主要考虑到我们学校没有自己的校园导航,有时会给同学及参观人员带来不便,又看到好多学校都有自己的导航,所以就从这个需求方 ...
- SQL SERVER中查询无主键的SQL
--生成表 IF EXISTS ( SELECT name FROM sysobjects WHERE xtype = 'u' ...
- 百度地图之POI
// // PoiViewController.m // baiDuDemo // // Created by City--Online on 15/6/4. // Copyright (c) 201 ...
- c++ ip地址相关
#include <stdio.h> #include <string.h> #include <arpa/inet.h> #include <sys/typ ...
- short-path problem (Floyd) 分类: ACM TYPE 2014-09-01 23:58 100人阅读 评论(0) 收藏
#include <cstdio> #include <iostream> #include <cstring> using namespace std; cons ...