Another Crisis

Here Insert Picture Description
UVA12186
definitions : d p [ i ] dp[i] is the first to make i i employees and their superiors letter requires a minimum number of workers, D P ( i ) DP(i) Returns d p [ i ] dp[i] values, S o n [ i ] Son[i] for the first i i all immediate subordinate employees.
Every employee has only one immediate superior, so there is no two workers from the same parent.

#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;
}
Published 25 original articles · won praise 27 · views 5586

Guess you like

Origin blog.csdn.net/qq_42971794/article/details/104035653