UVA 12186 Another Crisis

来源:互联网 发布:linux remount什么意思 编辑:程序博客网 时间:2024/05/20 10:21

Description 
Download as PDF

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 ( 1N105 , 1T100), 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(0Bii - 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



5

这几天看树形dp,只看懂了思想,却不会实现,唉,真是一脸懵逼

勉强跟着人家的代码写了写,不好意思啊

题意:有一个老板和n个员工,除了老板每个员工都有唯一的上司,老板编号为0,员工们为1-n,工人(没有下属的员工),要交一份请愿书,

但是不能跨级,当一个不是工人的员工接受到直系下属不少于T%的签字时,自己也会签字,并交给上级,问你最少有多少工人签字,才能让老板收到请愿书。

析:题意比较简单,也好理解,很明显是一个动态规划的题目,d(u)表示u给上级要发信至少需要多少工人签字。假设u有k个结点,那么至少要

c = (kT-1)/100 + 1个工人,然后把每个结点排序,找出最少的工人即可

#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<vector>#include<stack>#include<set>#include<map>#include<queue>#include<algorithm>using namespace std;int n,t;vector<int>sons[100005];int dp(int a){    if(sons[a].empty())        return 1;    int k=sons[a].size();    vector<int>d;    for(int i=0;i<k;i++)        d.push_back(dp(sons[a][i]));    sort(d.begin(),d.end());    int c=(k*t-1)/100+1;    int ans=0;    for(int i=0;i<c;i++)        ans+=d[i];    return ans;}int main(){    int i,j,k;    while(~scanf("%d %d",&n,&t)&&(n||t)){        for(i=0;i<=n;i++)            sons[i].clear();        int temp;        for(i=1;i<=n;i++){            scanf("%d",&temp);            sons[temp].push_back(i);        }        printf("%d\n",dp(0));    }    return 0;}


0 0
原创粉丝点击