您的位置:首页 > 其它

UVA 12186 Another Crisis [树形dp]

2016-08-01 12:21 411 查看
Another Crisis

Time Limit: 3000MS
4000
64bit IO Format: %lld & %llu

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

给一个员工关系树,然后要求出最少要多少个工人请愿。

对于节点u, 那么就有(child(u)*T-1) /100 + 1 个子节点请愿时,u也会请愿。

那么dfs一次,同时用priority_queue 维护最小的几个需要的值就好了。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<string>
#include<iomanip>
#include<ctime>
#include<climits>
#include<cctype>
#include<algorithm>
#ifdef WIN32
#define AUTO "%I64d"
#else
#define AUTO "%lld"
#endif
using namespace std;
#define smax(x,tmp) x=max((x),(tmp))
#define smin(x,tmp) x=min((x),(tmp))
#define maxx(x1,x2,x3) max(max(x1,x2),x3)
#define minn(x1,x2,x3) min(min(x1,x2),x3)
const int INF=0x3f3f3f3f;
const int maxn = 100005;
struct Edge
{
int to,next;
}edge[maxn];
int head[maxn];
int maxedge;
inline void addedge(int u,int v)
{
edge[++maxedge] = (Edge) { v,head[u] };
head[u] = maxedge;
}
int n,T;
bool init()
{
if(!~scanf("%d%d",&n,&T) || !n || !T) return false;
memset(head,-1,sizeof(head)); maxedge=-1;
for(int i=1;i<=n;i++)
{
int tmp;
scanf("%d",&tmp);
addedge(tmp,i);
}
return true;
}
int dp[maxn];
int dfs(int u)
{
priority_queue <int,vector<int>,greater<int> > que;
int child = 0;
int ret = 0;
for(int i=head[u];~i;i=edge[i].next)
{
child++;
int v = edge[i].to;
que.push(dfs(v));
}
if(!child) return 1;
int cnt = (child*T-1)/100 + 1; // -1 to avoid just perfect division
while(cnt--) ret += que.top() , que.pop();
return ret;
}
int main()
{
freopen("crisis.in","r",stdin);
freopen("crisis.out","w",stdout);
while(init())
printf("%d\n",dfs(0));
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  dp