HDU 4169 树形DP

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 = 151000;
const int inf = 99999999;
const int mod= 1000000007;

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=0;i<=k;i++) last[i]=dp[i];
    for(int i=0;i<G[x].size();i++) {
        int v=G[x][i];
        dfs(v,last);
    }
    for(int i=k;i>=1;i--) {
        if(dp[i-1]||!(i-1)) {
            dp[i]=max(last[i],dp[i-1]+a[x]);
        }
        else dp[i]=last[i];
    }
}
int main() {
    int u;
    while(scanf("%d%d",&n,&k)!=EOF) {
        for(int i=0;i<=n;i++) G[i].clear();
        for(int i=1;i<=n;i++) {
            scanf("%d%d",&u,&a[i]);
            if(u==0) root=i;
            else G[u].pb(i);
        }
        for(int i=0;i<=k;i++) dp[i]=0;
        dfs(root,dp);
        if(dp[k]) {
            cout<<dp[k]<<endl;
        }
        else printf("impossible\n");
    }
    return 0;
}
时间: 2024-10-12 16:38:30

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 encourage people to do sports. He has got trouble in choosing the route. There are N houses and N - 1 roads in his village. Each road connects two houses,

hdu 1250 树形DP

Anniversary party Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Appoint description:  System Crawler  (2014-07-27) Description There is going to be a party to celebrate the 80-th Anniversary of the Ural St

hdu 4276(树形dp)

题意:带权树上有起点终点每个点上有宝藏,一个人只有T分钟要从起点到重点,问你最多能收集多少宝藏. 思路:树形dp,首先判断能不能走到终点,然后把路径上的边权变为0时间减去所有边权.dp[v][j]表示从v出发回到v话费j分钟最多能收集到的宝藏. dp[v][j] = max(dp[v][j], dp[x][k] + dp[v][j-k-2*val]); 被G++卡了好长时间,换成c++就过了. 代码如下: 1 #include <stdio.h> 2 #include <string.h

hdu 5148 树形dp+分组背包问题

http://acm.hdu.edu.cn/showproblem.php?pid=5148 Problem Description Long long ago,there is a knight called JayYe.He lives in a small country.This country is made up of n cities connected by n-1 roads(that means it's a tree).The king wants to reward Ja

HDU 1520 树形dp裸题

1.HDU 1520  Anniversary party 2.总结:第一道树形dp,有点纠结 题意:公司聚会,员工与直接上司不能同时来,求最大权值和 #include<iostream> #include<cstring> #include<cmath> #include<queue> #include<algorithm> #include<cstdio> #define max(a,b) a>b?a:b using nam

hdu 3586 树形dp+二分

题目大意:给定n个敌方据点,1为司令部,其他点各有一条边相连构成一棵 树,每条边都有一个权值cost表示破坏这条边的费用,叶子节点为前线.现要切断前线和司令部的联系,每次切断边的费用不能超过上限limit,问切断所 有前线与司令部联系所花费的总费用少于m时的最小limit.1<=n<=1000,1<=m<=100万 题目要问的是最小的最大限制,必然二分答案 然后对于每一个值,树形DP判定是否可行 dp[i]表示要切断以i为根的其它所有子树的最小代价. 其中设定叶子结点的代价为无穷大

HDU 1561 树形DP入门

The more, The Better Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6011    Accepted Submission(s): 3555 Problem Description ACboy很喜欢玩一种战略游戏,在一个地图上,有N座城堡,每座城堡都有一定的宝物,在每次游戏中ACboy允许攻克M个城堡并获得里面的宝物

hdu 1561 树形dp

又一道树形dp,发现其实树形dp长得都挺像的. 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using namespace std; 5 6 const int INF = -9999999; 7 const int N = 201; 8 int dp[N][N]; 9 int head[N]; 10 int value[N]; 11 int n, m, e; 12 13 void i

hdu 1011 树形dp+背包

题意:有n个房间结构可看成一棵树,有m个士兵,从1号房间开始让士兵向相邻的房间出发,每个房间有一定的敌人,每个士兵可以对抗20个敌人,士兵在某个房间对抗敌人使无法走开,同时有一个价值,问你花费这m个士兵可以得到的最大价值是多少 分析:树形dp,对于点u,dp[u][j]表示以u为根的树消耗j个士兵得到的最大值,dp[i][j]=max(dp[i][j],dp[i][j-k]+dp[son][k]+val[u]) 注意是无向图,vis位置不能随便放,且注意dp不能直接+val,因为这样根节点就加不