UVA12186
定义:
d
p
[
i
]
dp[i]
dp[i]为要使第
i
i
i名员工向其上级发信最少需要多少工人,
D
P
(
i
)
DP(i)
DP(i)返回
d
p
[
i
]
dp[i]
dp[i]的值,
S
o
n
[
i
]
Son[i]
Son[i]为第
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;
}