Another Crisis

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

#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;
}
发布了25 篇原创文章 · 获赞 27 · 访问量 5586

猜你喜欢

转载自blog.csdn.net/qq_42971794/article/details/104035653