Wealthy Family | ||||||
| ||||||
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 | ||||||
Source | ||||||
The 2011 Rocky Mountain Regional Contest |
题目大意:
一共给你N个点,最多只能拿K个点。
需要保证所拿的各个点的祖先都不在这K个点中。
思路(树型dp是窝第噩梦啊):
首先分析出这是一个很裸的树背包。
那么设定dp【i】【j】表示到点i,拿了j个点的最大价值(当然满足祖先的约数条件情况下)。
那么不难推出其状态转移方程:
dp【i】【j+k】=max(dp【i】【j+k】,dp【i】【j】+dp【v】【k】);
但是因为点的个数比较多,显然二维数组的内存是开不下的。
那么考虑在Dfs过程中设定为一维即可。
时间判定比较严格,需要对子树中点的个数进行统计,直接暴力背包会TLE.
Ac代码:
#include<stdio.h>
#include<string.h>
#include<vector>
using namespace std;
vector<int >mp[150500];
int val[150500];
int dp[350];
int n,m;
int Dfs(int u)
{
int cur[350],tot=0;
for(int i=0;i<=m;i++)dp[i]=cur[i]=-0x3f3f3f3f;
cur[0]=dp[0]=0;
for(int i=0;i<mp[u].size();i++)
{
int v=mp[u][i];
int cnt=Dfs(v);
for(int j=tot;j>=0;j--)
{
for(int k=1;k<=cnt&&(k+j)<=m;k++)
{
cur[k+j]=max(cur[k+j],dp[k]+cur[j]);
}
}
tot=min(m,tot+cnt);
}
cur[1]=max(cur[1],val[u]);
for(int i=0;i<=m;i++)
{
dp[i]=cur[i];
}
if(mp[u].size()==0)tot=1;
return tot;
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
memset(dp,0,sizeof(dp));
for(int i=0;i<=n;i++)mp[i].clear();
for(int i=1;i<=n;i++)
{
int x;
scanf("%d%d",&x,&val[i]);
mp[x].push_back(i);
}
Dfs(0);
if(dp[m]<0)printf("impossible\n");
else printf("%d\n",dp[m]);
}
}