UVA12186 Another Crisis dp 题解

来源:互联网 发布:mac怎么移动文件 编辑:程序博客网 时间:2024/06/10 09:59

UVA12186 Another Crisis dp 题解

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3338

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

  1. 题目大意

公司有一个老板与n个员工。除老板外,每名员工有且仅有一个直属上司。除底层员工外,每位中级管理者可以同时管理多名员工。老板编号为0,员工与中级管理者编号为1~n。
现需要签署一份请愿书。当一个管理者,其直属下属中不小于T%的人签署时,该管理者也会签署,并递交上级。一次类推。
问:至少需要一共多少人签署请愿书,该请愿书才会最终递交到老板手中?

  1. 解题思路
    设d[u]表示u管理者需要签署的人数。假设u有k个直接下属,则至少需要c=(kT-1)/100+1个直接下属的签署。将所有d[u]按照从小到大排序后,输出前c个的和。d[0]即为最终答案。
  2. 代码片段
// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。////#include "stdafx.h"//VS2015编译器下不可省略#include<iostream>#include<algorithm>#include<vector>using namespace std;#define REN(i, n) for(int i = 0; i < n; i++)#define ALL(a) a.begin(), a.end()const int maxn = 100000 + 5;int n, T;vector<int> sons[maxn];//存放第i个管理者的直接下属编号int dp(int u){    if (sons[u].empty())return 1;//如果没有为空,表示没有下属,                                //即为底层员工。那么只要员工自身签字就好    int k = sons[u].size();//k存放直属下属个数    vector<int> d;//存放直属下属需要的签署人数    REN(i, k)d.push_back(dp(sons[u][i]));//递归计算    sort(ALL(d));//排序    int c = (k*T - 1) / 100 + 1;    int ans = 0;    REN(i, c) ans += d[i];//计算前c个的和    return ans;}int main(){    int f;    while (cin >> n >> T && n)    {        REN(i, n + 1)sons[i].clear();//初始化每一个结点        for (int i = 1; i <= n; i++)        {            cin >> f;            sons[f].push_back(i);//将直属下属与上司对号入座        }        cout << dp(0) << endl;    }    return 0;}
0 0
原创粉丝点击