HDU 4169 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

关键是如何开出dp数组。

#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<string>
#include<cstring>
#include<map>
using namespace std;
const int Maxn = 150005;
const int maxn = 305;
vector<int> tree[Maxn];
int n, x, y, dp[maxn], tot, m, c[Maxn], root;

int work(int x)
{
	int f[maxn], tot = 0;
	for (int i = 0; i <= m; i++) dp[i] = f[i] = -min(i, 1);
	for (int i = 0; i < tree[x].size(); i++)
	{	
		int z = work(tree[x][i]);
		for (int j = tot; j >= 0;j--)
			for (int k = 1; k <= z; k++)
				if (j + k <= m&&f[j] >= 0) f[j + k] = max(f[j + k], f[j] + dp[k]);
		tot = min(tot + z, m);
	}
	f[1] = max(f[1], c[x]);
	tot = max(tot, 1);
	for (int i = 0; i <= tot; i++) dp[i] = f[i];
	return tot;
}

int main()
{
	while (~scanf("%d%d", &n, &m))
	{
		for (int i = 0; i <= n; i++) tree[i].clear();
		for (int i = 1; i <= n; i++)
		{
			scanf("%d%d", &x, &c[i]);
			if (!x) root = i;
			else tree[x].push_back(i);
		}
		work(root);
		if (dp[m] >= 0) cout << dp[m] << endl;
		else cout << "impossible\n";
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值