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;
}

 

posted @ 2015-12-20 17:31  meekyan  阅读(227)  评论(0编辑  收藏  举报