【算法竞赛入门经典】树形DP 例题9-12 UVa12186
2018-03-07 16:18
573 查看
【算法竞赛入门经典】树形DP 例题9-12 UVa12186
【算法竞赛入门经典】树形DP 例题9-12 UVa12186例题UVa12186
分析
样例实现代码
结果
例题UVa12186
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
分析
这是一个树形的DP,使用递归的方法实现。利用vector数组储存每个节点的孩子节点【直属的下属】
在每一层中利用一个vector用于统计这个下属签字至少需要多少工人,然后使用sort对vector排序
Tip:vector排序使用
sort(vector::begin(),vector::end());
之后根据比例T挑选出数量。注意,本题目显然是要求向上取整,因此,使用了技巧
C=(k*T-1)/100+1来计算
另外:鉴于本题的树结构,不需要进行记忆化搜索。
样例实现代码
#include<iostream> #include <fstream> #include<vector> #include<algorithm> #define maxn 100000+5 using namespace std; vector<int>sons[maxn]; int n,T; int dp(int u){ if(sons[u].empty()){ return 1; } int k=sons[u].size(); vector<int>d; for(int i=0;i<k;i++){ d.push_back(dp(sons[u][i])); } sort(d.begin(),d.end()); int c=(k*T-1)/100+1; int sum=0; for(int i=0;i<c;i++) sum+=d[i]; return sum; } int main(){ int temp; ofstream SaveFile("cpp-home.txt"); while(cin>>n>>T&&n){ for(int i=0;i<=n;i++) sons[i].clear(); for(int i=1;i<=n;i++){ cin>>temp; sons[temp].push_back(i); } int ans=dp(0); cout<<ans<<endl; SaveFile<<ans<<endl; } SaveFile.close(); return 0; }
结果
相关文章推荐
- 【算法竞赛入门经典】树形DP的状态转移方程优化 例题9-14 UVa1218
- 【算法竞赛入门经典】类LCS动态规划;指标函数分解 例题9-7 UVa1625
- 【算法竞赛入门经典】多阶段决策问题 例题9-4 UVa116
- 算法竞赛入门经典 例题7-2 埃及分数
- 【算法竞赛入门经典】6.5[图的概念和拓扑序] 例题6-21 UVa506
- 算法竞赛入门经典第6章例题(2):二叉树部分+四分树
- 【算法竞赛入门经典】DAG上的动态规划 例题9-1 UVa1025
- [刷题]算法竞赛入门经典 3-12/UVa11809
- 【算法竞赛入门经典】树的最大独立集、树的唯一性问题 例题9-13 UVa1220
- 【算法竞赛入门经典】集合的动态规划;位运算 例题9-15 UVa10817
- 算法竞赛入门经典 例题 3-2 蛇形填数
- UVa 133 算法竞赛入门经典4-3例题(约瑟夫环类型)
- 【算法竞赛入门经典】动态规划初步 例题9-6 UVa11400
- 【算法竞赛入门经典】动态规划初步 例题9-7 UVa11584
- 【算法竞赛入门经典】类似于最优矩阵链乘的动态规划 例题9-9 UVa10003
- 【算法竞赛入门经典】6.4.2用BFS求最短路 例题6-14 UVa816
- 【算法竞赛入门经典】DAG上的动态规划 例题9-2 UVa437
- 算法竞赛入门经典 例题 3-1 开灯问题
- 【算法竞赛入门经典】7.6 迭代加深搜索与IDA* 例题7-10 UVa11211
- 算法竞赛入门经典 例题9-1