在这里插入图片描述
UVA12186
定义dp[i]dp[i]为要使第ii名员工向其上级发信最少需要多少工人,DP(i)DP(i)返回dp[i]dp[i]的值,Son[i]Son[i]为第ii名员工的所有直属下级。
每一个员工都只有一个直接上级,因此不会有两个工人由同一上级。

#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
using namespace std;
int N, T;
vector<int> Son[100001];
void Clear() {
	for (int i = 0; i <= N; ++i) {
		Son[i].clear();
	}
}
bool Input() {
	cin >> N >> T;
	Clear();
	if (!N && !T) {
		return false;
	}
	for (int SonNode = 1; SonNode <= N; ++SonNode) {
		int BossNode;
		cin >> BossNode;
		Son[BossNode].push_back(SonNode);
	}
	return true;
}
int DP(int ID) {
	//如果该员工为工人,返回1(自己同意)
	if (Son[ID].empty()) {
		return 1;
	}
	int Len = Son[ID].size();
	int* dp = new int[Len];
	//求该员工的所有下属的dp值
	for (int i = 0; i < Len; ++i) {
		dp[i] = DP(Son[ID][i]);
	}
	//排个序
	sort(dp, dp + Len);
	//求该员工最少要至少多少下属同意
	int&&LeastAgreeDirectReports = ceil(static_cast<double>(Len * T) / 100.);
	int&& Ans = 0;
	//选择所需工人数目最少的LeastAgreeDirectReports个下属,得到该员工对应的最少员工
	for (int i = 0; i < LeastAgreeDirectReports; ++i) {
		Ans += dp[i];
	}
	delete dp;
	return Ans;
}
int main() {
	while (Input()) {
		cout << DP(0) << endl;
	}
	return 0;
}
posted on 2020-01-19 02:26  SCU_GoodGuy  阅读(127)  评论(0)    收藏  举报