Uva--12186(树形动规)

2014-08-28 13:36:45

12186 - Another Crisis

A couple of years ago, a new world wide crisis started, leaving many people with economical problems. Some workers of a particular company are trying to ask for an increase in their salaries.

The company has a strict hierarchy, in which each employee has exactly one direct boss, with the exception of the owner of the company that has no boss. Employees that are not bosses of any other employee are calledworkers. The rest of the employees and the owner are called bosses.

To ask for a salary increase, a worker should file a petition to his direct boss. Of course, each boss is encouraged to try to make their subordinates happy with their current income, making the company's profit as high as possible. However, when at least T percent of its direct subordinates have filed a petition, that boss will be pressured and have no choice but to file a petition himself to his own direct boss. Each boss files at most 1 petition to his own direct boss, regardless on how many of his subordinates filed him a petition. A boss only accounts his direct subordinates (the ones that filed him a petition and the ones that didn't) to calculate the pressure percentage.

Note that a boss can have both workers and bosses as direct subordinates at the same time. Such a boss may receive petitions from both kinds of employees, and each direct subordinate, regardless of its kind, will be accounted as 1 when checking the pressure percentage.

When a petition file gets all the way up to the owner of the company, all salaries are increased. The workers' union is desperately trying to make that happen, so they need to convince many workers to file a petition to their direct boss.

Given the company's hierarchy and the parameter T, you have to find out the minimum number of workers that have to file a petition in order to make the owner receive a petition.

Input 

There are several test cases. The input for each test case is given in exactly two lines. The first line contains two integers N and T ( 1$ \le$N$ \le$105 , 1$ \le$T$ \le$100), separated by a single space. N indicates the number of employees of the company (not counting the owner) and T is the parameter described above. Each of the employees is identified by an integer between 1 and N. The owner is identified by the number 0. The second line contains a list of integers separated by single spaces. The integer Bi, at position i on this list (starting from 1), indicates the identification of the direct boss of employee i (0$ \le$Bi$ \le$i - 1).

The last test case is followed by a line containing two zeros separated by a single space.

Output 

For each test case output a single line containing a single integer with the minimum number of workers that need to file a petition in order to get the owner of the company to receive a petition.

Sample Input 

3 100 
0 0 0 
3 50 
0 0 0 
14 60 
0 0 1 1 2 2 2 5 7 5 7 5 7 5 
0 0 

Sample Output 

3 
2 
5

思路:树形DP,小白书例题,dp[u]表示让 u 给上级发信最少需要多少个工人。则至少需要 need = (k * T - 1) / 100 + 1 个直接下属发信才行。那么把所有子节点 dp[v] 从小到大排序,前 need 个加起来即可。最终的答案就是dp[0],由于排序,算法时间复杂度为O(nlogn)。
 1 /*************************************************************************
 2     > File Name: 12186.cpp
 3     > Author: Nature
 4     > Mail: 564374850@qq.com 
 5     > Created Time: Thu 28 Aug 2014 12:41:53 PM CST
 6 ************************************************************************/
 7 
 8 #include <cstdio>
 9 #include <cstring>
10 #include <cstdlib>
11 #include <cmath>
12 #include <vector>
13 #include <iostream>
14 #include <algorithm>
15 using namespace std;
16 const int INF = 1 << 30;
17 typedef long long ll;
18 
19 int N,T,b;
20 vector<int> ver[100005];
21 
22 int Dp(int u){
23     int len = ver[u].size();
24     if(len == 0)
25         return 1;
26     vector<int> d;
27     for(unsigned int i = 0; i < len; ++i){
28         d.push_back(Dp(ver[u][i]));
29     }
30     int need = (len * T - 1) / 100 + 1;
31     sort(d.begin(),d.end());
32     int ans = 0;
33     for(int i = 0; i < need; ++i) ans += d[i];
34     return ans;
35 }
36 
37 int main(){
38     while(scanf("%d%d",&N,&T) == 2){
39         if(N == 0 && T == 0)
40             break;
41         for(int i = 0; i <= N; ++i) ver[i].clear();
42         for(int i = 1; i <= N; ++i){
43             scanf("%d",&b);
44             ver[b].push_back(i);
45         }
46         printf("%d\n",Dp(0));
47     }
48     return 0;
49 }
 
 
 

转载于:https://www.cnblogs.com/naturepengchen/articles/3941639.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值