UVA

来源:互联网 发布:excel 设置数据有效性 编辑:程序博客网 时间:2024/06/08 09:43

原题:

A couple of years ago, a new world wide crisis started, leaving many p eople 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 b oss, with the
exception of the owner of the company that has no b oss. Employees that are not b osses of any other
employee are called workers. The rest of the employees and the owner are calledbosses.
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 leastT 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 10 5 , 1 T 100), separated by a single space.N indicates
the number of employees of the company (not counting the owner) andT is the parameter described
above. Each of the employees is identified by an integer between 1 andN. The owner is identified by
the number 0. The second line contains a list of integers separated by single spaces. The integerB i,
at position i on this list (starting from 1), indicates the identification of the direct boss of employeei
(0 B ii1).
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
325


题意:

       一个老板和N个员工,每个员工都有直属上司,工人们要逐级向上给老板一份请愿书,每个中级员工的下属中有不小于T%的员工签名时,中级员工会签名并向上提交。最少多少工人签字会使老板收到请愿书。


思路:

       取dp[u]为u给上层传递最少需要多少工人,若u有k个直接下属,则需c=(k*T-1)*100+1个下属签名。则k个下属的dp值有小到大排序,前c个下属的dp值相加为u向上传递所需的工人数,即dp[u]。则dp[0]即为答案。


#include <iostream>#include <iomanip>#include <algorithm>#include <cstdio>#include <cstring>#include <queue>#include <deque>#include <string>#include <cmath>#include <vector>#include <utility>#include <set>#include <climits>//#pragma comment(linker, "/STACK:1024000000,1024000000")#define INF 2147483647using namespace std;typedef long long ll;int N,i,j,T;vector <int> sons[100005];int solve(int u){    if(sons[u].empty())        return 1;    int k=sons[u].size();    vector<int> dp;    for(int z=0;z<k;z++)        dp.push_back(solve(sons[u][z]));    sort(dp.begin(),dp.end());    int c=(k*T-1)/100+1;    int ans=0;    for(int y=0;y<c;y++)        ans+=dp[y];    return ans;}int main(){    while(scanf("%d%d",&N,&T)&&N&&T)    {        for(i=0;i<100005;i++)            sons[i].clear();        for(i=1;i<=N;i++)        {            int num;            scanf("%d",&num);            sons[num].push_back(i);//每个中层员工的下属编号        }        int res=solve(0);        printf("%d\n",res);    }    return 0;}


原创粉丝点击