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. java正则API简单解析

    java.util.regex是一个用正则表达式所订制的模式来对字符串进行匹配工作的类库包. 1.简介: java.util.regex是一个用正则表达式所订制的模式来对字符串进行匹配工作的类库包. ...

  2. [JAVA设计模式]第二部分:创建模式

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

  3. DBCP JAVA 连接池

    package com.sinoglobal.db; import java.sql.Connection; import java.sql.DriverManager; import java.sq ...

  4. Linux_常用命令_02

    1. 配置网络参数: (1).root登录 --> setup命令 进入到 "text mode setup utiliy" (2).运行命令"/etc/rc.d/ ...

  5. html5 的draggable属性使用<转载收藏>

    在HTML5中,已经支持在浏览器与其他应用程序之间的数据互相拖动,同时也大大简化了有关于拖放方面的代码. 实现拖放的步骤 在HTML5中要想实现拖放操作,至少要经过两个步骤: 将想要拖放的对象元素的d ...

  6. 04 SQL是关于集合的

    面向集合去思考 要想成为写SQL语句的高级专家, 最困难的是一个转变就是从面相过程的思维方式转变到面相集合的思维方式. 首先要停止那些一次处理一行数据的过程化步骤思维, 试着把思路转移到使用类似于 “ ...

  7. Jmeter使用之常用函数介绍

    “_csvRead”函数 CsvRead函数是从外部读取参数,CsvRead函数可以从一个文件中读取多个参数. 下面具体讲一下如何使用csvread函数: 1.     新建一个csv或者text文件 ...

  8. django连接mysql自动同步生成数据表

    python manage.py makemigrations python manage.py migrate 如果是 Django 不主动提示创建管理员(Django 1.9不提示)用下面的命令创 ...

  9. 关于mysql的基础知识

    一.数据库的简介 什么是数据库? 数据的仓库,如:在atm的实例中我们创建一个db目录称之为数据库 什么是 MySQL.Oracle.SQLite.Access.MS SQL Server等 ? 他们 ...

  10. 【浏览器渲染原理】渲染树构建之渲染树和DOM树的关系(转载 学习中。。。)

    在DOM树构建的同时,浏览器会构建渲染树(render tree).渲染树的节点(渲染器),在Gecko中称为frame,而在webkit中称为renderer.渲染器是在文档解析和创建DOM节点后创 ...