树上的动态规划学习2 - Another Crisis (Uva12186)

Another Crisis

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

代码分递归和非递归两个版本。

递归版本是直接参考紫书的。

#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;
const  int maxn=100000+5;
int n,T;
vector<int> sons[maxn];

int dp(int u) {
    if (sons[u].empty()) return 1;
    vector<int> d;

    for (int i=0; i<sons[u].size(); i++)
        d.push_back(dp(sons[u][i]));

    sort(d.begin(), d.end());
    int c = (sons[u].size()*T-1)/100 + 1;
    int ans = 0;

    for (int i=0; i<c; i++)
        ans+=d[i];

    return ans;
}


int main()
{
    int boss;
    while(scanf("%d%d", &n, &T)==2 && n) {
        for (int i=0; i<=n; i++)
             sons[i].clear();
        for (int i=1; i<=n; i++) {
             scanf("%d", &boss);
             sons[boss].push_back(i);
        }
        cout<<dp(0)<<endl;
    }

    return 0;
}

有几点要注意:
1) 
c=(KT-1)/100+1 是表示不小于KT/100的最小整数。比如说KT=33, 那么KT/100=0, c=1。KT=1, KT/100=0, c=1。
那么为何不直接用KT/100+1呢?因为如果KT/100刚好为1的话,那么c=2,但实际上c应该等于1。
2)初始化sons[]的时候i从0开始,因为老板的sons也要初始化为0。但给sons[]赋值的时候i从1开始,比如说0,3,5,说明员工1的老板是0,员工2的老板是3,员工3的老板是5。
3) 因为题目说要最少所需的员工签字,所以要对d数组sort,找出前面c个最小的数值来。

 

 

 

非递归版本是我写的,用栈来实现。这个代码可能不是最优,因为还加上了parent[]数组,不过好歹还是被接受了。如果大家有更好的欢迎留言。

#include<iostream>
#include<vector>
#include<algorithm>
#include <stack>

using namespace std;
const  int maxn=100000+5;
int n,T;
vector<int> sons[maxn];
int parent[maxn] = {0};

stack<int> s;
int ans[maxn] = {0};

int dp_non_recursion() {
    vector<int> d;

    //从老板开始,依次将儿子压栈
    s.push(0);

    for (int i=0; i<n; i++)
        for (int j=0; j<sons[i].size(); j++)
                s.push(sons[i][j]);

    while(!s.empty()) {
        d.clear();
        int t = s.top();
        s.pop();
        if (sons[t].size()==0)
            ans[t]=1;
        else{
            for (int i=0; i<sons[t].size(); i++) {
                d.push_back(ans[sons[t][i]]);
            }
            sort(d.begin(), d.end());
            int c = (sons[t].size()*T-1)/100 + 1;
            for (int i=0; i<c; i++)
                ans[t]+=d[i];
        }
    }

    return ans[0];
}


int main()
{
    int boss;
    while(scanf("%d%d", &n, &T)==2 && n) {
        for (int i=0; i<=n; i++) {
             sons[i].clear();
             ans[i]=0;
        }
        for (int i=1; i<=n; i++) {
            scanf("%d", &boss);
            parent[i]=boss;
            sons[boss].push_back(i);
        }
        cout<<dp_non_recursion()<<endl;
    }
    return 0;
}

注意不管是递归还是非递归,我们都不需要用到vis[],因为每个节点都刚好处理一次,没有重复访问的情况。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值