UVA12186--- Another Crisis (树形dp)
2015-05-28 22:04
501 查看
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 ( 1≤ \leN≤ \le105 , 1≤ \leT≤ \le100), 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≤ \leBi≤ \lei - 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%的人签字,问让老板签字最少需要多少工人签字
dp[u]表示让u签字,需要的最少的工人数目
对于他的子节点v的dp[v],从小到大排序,然后取ceil(T * 1.0 / 100 * size)个
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 ( 1≤ \leN≤ \le105 , 1≤ \leT≤ \le100), 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≤ \leBi≤ \lei - 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%的人签字,问让老板签字最少需要多少工人签字
dp[u]表示让u签字,需要的最少的工人数目
对于他的子节点v的dp[v],从小到大排序,然后取ceil(T * 1.0 / 100 * size)个
/************************************************************************* > File Name: uva12186.cpp > Author: ALex > Mail: zchao1995@gmail.com > Created Time: 2015年05月28日 星期四 21时54分13秒 ************************************************************************/ #include <functional> #include <algorithm> #include <iostream> #include <fstream> #include <cstring> #include <cstdio> #include <cmath> #include <cstdlib> #include <queue> #include <stack> #include <map> #include <bitset> #include <set> #include <vector> using namespace std; const double pi = acos(-1.0); const int inf = 0x3f3f3f3f; const double eps = 1e-15; typedef long long LL; typedef pair <int, int> PLL; static const int N = 100100; int dp ; vector <int> tree ; vector <int> use; int n, T; void dfs(int u) { int size = tree[u].size(); if (!size) { dp[u] = 1; return; } for (int i = 0; i < size; ++i) { int v = tree[u][i]; dfs(v); } use.clear(); for (int i = 0; i < size; ++i) { use.push_back(dp[tree[u][i]]); } sort(use.begin(), use.end()); int cnt = ceil(T * 1.0 / 100 * size); for (int i = 0; i < cnt; ++i) { dp[u] += use[i]; } } int main() { while (~scanf("%d%d", &n, &T)) { if (!n && !T) { break; } for (int i = 0; i <= n; ++i) { tree[i].clear(); } int u; for (int i = 1; i <= n; ++i) { scanf("%d", &u); tree[u].push_back(i); dp[i] = 0; } dp[0] = 0; dfs(0); printf("%d\n", dp[0]); } return 0; }
相关文章推荐
- BZOJ 1816: [Cqoi2010]扑克牌( 二分答案 )
- Junit安装及使用
- PHP Official Account Services Unavailable, Try Again Later
- Song -----See You Again
- HTTP错误代码
- 【从0到1学Web前端】CSS定位问题二(float和display的使用)
- 【从0到1学Web前端】CSS定位问题二(float和display的使用)
- RAP开发入门-运行过程简析(三)
- python基础教程学习笔记---(4)字典
- (转)多进程 & 多线程的区别与适用场景
- 【从0到1学Web前端】CSS定位问题二(float和display的使用) 分类: HTML+CSS 2015-05-28 22:03 812人阅读 评论(1) 收藏
- 【VBA】 通过VBA脚本将EXCEL的数据导入 ORACLE
- 一些项目——抱歉
- 创建基线或发行基线的主要步骤
- 第二次冲刺(4)
- 【转载】failed to create virtual machine
- 携程瘫痪非偶然 国内互联网企业漠视数据管理
- leetcode | Letter Combinations of a Phone Number
- C#程序开发中经常遇到的10条实用的代码
- Merge Sorted Array