【UVA 12186】 Another Crisis(树形dp)

博客围绕公司员工加薪请愿问题展开,员工需向直属老板请愿,当老板的至少T%直属下属请愿时,老板会向上请愿直至老板。题目给出公司层级和T值,要求找出让老板收到请愿的最少工人数。采用树形dp思路,从老板开始递归求解,同时考虑浮点误差。

题目

Description

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 called workers. 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 ≤ N ≤ 105
, 1 ≤ T ≤ 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 ≤ Bi ≤ 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

题目大意:一个人如果签字的话,他的直属下属里必须有至少T%的人签字,要让老板签字最少需要多少下属签字。
原题传送门(vjudge)

思路

树形 d p dp dp
从老板开始,递归求得答案即可。
有一点,我们要考虑到浮点误差,所以将 k × T ÷ 100 k \times T \div 100 k×T÷100 写为 ( k × T − 1 ) ÷ 100 + 1 (k \times T - 1) \div 100 + 1 (k×T1)÷100+1
完事,上代码。

代码

#include <bits/stdc++.h>

using namespace std; 
const int M = 100005;
int a, n, t;
vector <int> s[M];
 
int search(int u) {
    if (s[u].empty()) return 1;
    int k = s[u].size();
    vector <int> dp;
    for (int i = 0; i < k; i++)
        dp.push_back(search(s[u][i]));
    sort(dp.begin(), dp.end());
    int c = (k*t-1)/100.0+1, ans = 0;
    for (int i = 0; i < c; i++) ans += dp[i];
    return ans;
}

int main() {
    ios::sync_with_stdio(false);
    while (cin >> n >> t, n+t != 0) {
        for (int i = 0; i <= n; i++) s[i].clear();
        for (int i = 1; i <= n; i++)
            cin >> a, s[a].push_back(i);
        cout << search(0) << endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值