紫书 例题 9-12 UVa 12186 (树形dp)

这道题还是比较简单的,对于当前节点,算出每个儿子需要的人数

然后再算出当前节点需要多少个人数,然后排个序加上去就好了。

#include<cstdio>
#include<vector> 
#include<algorithm>
#define REP(i, a, b) for(int i = (a); i < (b); i++)
using namespace std;

const int MAXN = 112345;
vector<int> son[MAXN];
int T, n;

int dp(int u)
{
	if(son[u].empty()) return 1;
	int k = son[u].size();
	vector<int> t;
	REP(i, 0, k)
		t.push_back(dp(son[u][i]));
	sort(t.begin(), t.end());
	int c = (k * T - 1) / 100 + 1, ans = 0;
	REP(i, 0, c)
		ans += t[i];
	return ans;
}

int main()
{
	while(~scanf("%d%d", &n, &T) &&n)
	{
		REP(i, 0, n + 1) son[i].clear();
		REP(i, 1, n + 1)
		{
			int x;
			scanf("%d", &x);
			son[x].push_back(i);
		}
		printf("%d\n", dp(0));
	}
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/qq_34416123/article/details/81431720
今日推荐