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的更多相关文章

  1. Uva 12186 工人的请愿书

    题目链接:https://uva.onlinejudge.org/external/121/12186.pdf 题意: 给出一个树状关系图,公司里只有一个老板编号为0,其他人员从1开始编号.除了老板, ...

  2. UVa 12186 - Another Crisis(树形DP)

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

  3. UVa 12186 Another Crisis

    题意: 给出一个树状关系图,公司里只有一个老板编号为0,其他人员从1开始编号.除了老板,每个人都有一个直接上司,没有下属的员工成为工人. 工人们想写一份加工资的请愿书,只有当不少于员工的所有下属的T% ...

  4. UVa 12186 树形dp

    题意  分析   白皮书 P282  例题9-12 AC代码 #include <stdio.h> #include <math.h> #include <string. ...

  5. UVA - 12186 Another Crisis (树形DP)

    思路:dp[i]表示让上司i签字至少需要多少工人签字.       转移方程:将i的所有节点根据所需工人数量升序排序,设i需要k个下属签字,dp[i] = sum{dp[v]| 0 <= v & ...

  6. UVa 12186 工人的请愿书(树形DP)

    https://vjudge.net/problem/UVA-12186 题意: 一个老板和n个员工组成树状结构,每个员工都有自己的唯一上司,老板的编号为0,员工1~n,工人们打算签署一个志愿书给老板 ...

  7. UVa 12186 Another Crisis (DP)

    题意:有一个老板和n个员工,除了老板每个员工都有唯一的上司,老板编号为0,员工们为1-n,工人(没有下属的员工),要交一份请愿书, 但是不能跨级,当一个不是工人的员工接受到直系下属不少于T%的签字时, ...

  8. 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. 紫书 例题 9-12 UVa 12186 (树形dp)

    这道题还是比较简单的,对于当前节点,算出每个儿子需要的人数 然后再算出当前节点需要多少个人数,然后排个序加上去就好了. #include<cstdio> #include<vecto ...

随机推荐

  1. [SAP ABAP开发技术总结]OK_CODE

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  2. JQ将数组转换为Json

    var ArrComList; try { //接口传进来的数据格式为 A,B,C,D,这里根据逗号分隔返回数组. ArrComList = WeighControl.GetComList().spl ...

  3. hibernate缓存说明

    hibernate缓存说明: 1.一级缓存(session级别缓存)     一级缓存,不是用来提升性能,是用来处理事务的 2.二级缓存(sessionFactory级别缓存):     二级缓存,对 ...

  4. css position 绝对定位和相对定位

    position:absolute这个是绝对定位:是相对于浏览器的定位.比如:position:absolute:left:20px;top:80px; 这个容器始终位于距离浏览器左20px,距离浏览 ...

  5. Javascript 简单学习

    一直就崇拜JS如此牛叉的来操作Html标签, 抽出时间来学习学习,哈哈. Js是Netscape公司语言, 基于对象和事件驱动. 与Java不同之处: 1:所属公司不同 2:js是基于对象,java是 ...

  6. <<易货>>项目Postmortem结果

    设想和目标 我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 一开始想做的事情还是太多,没有形成整个app的核心功能,浪费了很多时间. 是否有充足的时间来做计划? 有 ...

  7. iOS - Swift 异常处理

    前言 在 Swift 1.0 时代是没有异常处理和抛出机制的,如果要处理异常,要么使用 if else 语句或 switch 语句判断处理,要么使用闭包形式的回调函数处理,再要么就使用 NSError ...

  8. struts2 if正确标签示例

    下面总结一下struts2 中if标签的使用 (1)判断字符串是否为空 <s:if test="user.username==null or user.username==''&quo ...

  9. HIHO线段树(成段)

    #include <stdio.h> #define lson l,mid,id<<1 #define rson mid+1,r,id<<1|1 ; ],lazy[ ...

  10. spring事务知识

    事务的传播行为? 在Spring 的事务中, _可以通过 propagation 来定义事务的传播行为_: PROPAGATION_required:如果当前没有事务,就新建一个事务,如果已经存在一个 ...