uva 12186
12186 - Another Crisis
Time limit: 3.000 seconds
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
3
2
5 这是我正式的第一次做树形dp的题,入门题,并不难,树形dp说到底就是在树结构上进行动态优化。
#include <cstdio>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
#define ll long long
#define _cle(m, a) memset(m, a, sizeof(m))
#define repu(i, a, b) for(int i = a; i < b; i++)
#define MAXN 100005
int n, T;
vector<int> v[MAXN]; int dp(int u)
{
if(v[u].empty()) return ;
int k = v[u].size();
vector<int> d;
repu(i, , k) d.push_back(dp(v[u][i]));
sort(d.begin(), d.end());
int c = (k * T - ) / + ;
int ans = ;
repu(i, , c) ans += d[i];
return ans;
} int main()
{
while(~scanf("%d%d", &n, &T) && (n + T))
{
repu(i, , n + ) v[i].clear();
int t;
repu(i, , n + ) {
scanf("%d", &t);
v[t].push_back(i);
} printf("%d\n", dp());
}
return ;
}
uva 12186的更多相关文章
- Uva 12186 工人的请愿书
题目链接:https://uva.onlinejudge.org/external/121/12186.pdf 题意: 给出一个树状关系图,公司里只有一个老板编号为0,其他人员从1开始编号.除了老板, ...
- UVa 12186 - Another Crisis(树形DP)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVa 12186 Another Crisis
题意: 给出一个树状关系图,公司里只有一个老板编号为0,其他人员从1开始编号.除了老板,每个人都有一个直接上司,没有下属的员工成为工人. 工人们想写一份加工资的请愿书,只有当不少于员工的所有下属的T% ...
- UVa 12186 树形dp
题意 分析 白皮书 P282 例题9-12 AC代码 #include <stdio.h> #include <math.h> #include <string. ...
- UVA - 12186 Another Crisis (树形DP)
思路:dp[i]表示让上司i签字至少需要多少工人签字. 转移方程:将i的所有节点根据所需工人数量升序排序,设i需要k个下属签字,dp[i] = sum{dp[v]| 0 <= v & ...
- UVa 12186 工人的请愿书(树形DP)
https://vjudge.net/problem/UVA-12186 题意: 一个老板和n个员工组成树状结构,每个员工都有自己的唯一上司,老板的编号为0,员工1~n,工人们打算签署一个志愿书给老板 ...
- UVa 12186 Another Crisis (DP)
题意:有一个老板和n个员工,除了老板每个员工都有唯一的上司,老板编号为0,员工们为1-n,工人(没有下属的员工),要交一份请愿书, 但是不能跨级,当一个不是工人的员工接受到直系下属不少于T%的签字时, ...
- UVa 12186 Another Crisis 工人的请愿书
c表示某上司上报的最少请愿下属,k表示总下属c=0.01T*k=kT/100(0.01T*k是整数)c=[0.01T*k]+1=[kT/100]+1(0.01T*k不是整数) kT=100 c=1 k ...
- 紫书 例题 9-12 UVa 12186 (树形dp)
这道题还是比较简单的,对于当前节点,算出每个儿子需要的人数 然后再算出当前节点需要多少个人数,然后排个序加上去就好了. #include<cstdio> #include<vecto ...
随机推荐
- [SAP ABAP开发技术总结]消息处理Messages
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- C#开发ActiveX控件及指纹采集
最近做一个指纹采集和比对的功能,因为公司整个项目是WEB类型的,所以指纹采集的模块要嵌套在网页中,那只有用ActiveX了,以下是一些操作及效果,做个笔记! 新建用户控件,编写CS代码,如下: usi ...
- Codeforces Round #250 (Div. 2)A(英语学习)
链接:http://codeforces.com/contest/437/problem/A A. The Child and Homework time limit per test 1 secon ...
- 在.bashrc文件中定义函数
在命令行上直接定义shell函数的明显缺点是当退出shell时,函数就消失了,对于复杂的函数来说,这可能会是个问题. 一个简单的方法就是每次启动新shell的时候都会自动加载所需要的函数. 最好的办法 ...
- iOS - Swift Subscript 下标脚本
1.Subscript 下标脚本允许你通过在实例后面的方括号中传入一个或者多个的索引值来对实例进行访问和赋值.语法类似于实例方法和计算型属性的混合.与定义实例方法类似,定义下标脚本使用 subscri ...
- 安装64位版Oracle11gR2后无法启动SQLDeveloper的解决方案(原创) (2016-10-29 下午01:56)
安装64位版Oracle11gR2后发现启动SQL Developer时弹出配置java.exe的路径,找到Oracle自带java.exe后产生的路径"C:\app\用户名\product ...
- python一个注意的地方
https://www.zhihu.com/question/25874136 class test: l=[] def init(self): self.l=['1','2','7'] a1=tes ...
- 【转载】Spark系列之运行原理和架构
参考 http://www.cnblogs.com/shishanyuan/p/4721326.html 1. Spark运行架构 1.1 术语定义 lApplication:Spark Applic ...
- intersection-of-two-arrays-ii
https://leetcode.com/problems/intersection-of-two-arrays-ii/ class Solution { public: vector<int& ...
- Service 与 Thread 的区别
很多时候,你可能会问,为什么要用 Service,而不用 Thread 呢,因为用 Thread 是很方便的,比起 Service 也方便多了,下面我详细的来解释一下. 1). Thread:Thre ...