UVA12186 - Another Crisis

设d[u]表示让u给上级发信最少需要多少个工人。假设u有k个子节点,则至少需要s = ceil(k * t / 100.0)个直接下属发信.所以要把这k个子节点的d值排序,把前s个值加起来就是d[u]的值,最终答案为d[0]

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <map>
#include <vector>
#include <cmath>
using namespace std;

#define maxn 100000 + 5
vector<int> v[maxn];
int t;
int dp(int n)
{
    if(v[n].empty())
        return 1;
    int s = ceil(v[n].size() * t / 100.0);
    vector<int> p;
    for(int i = 0; i < v[n].size(); i++)
    {
        p.push_back(dp(v[n][i]));
    }
    int ans = 0;
    sort(p.begin(), p.end());
    for(int i = 0; i < s; i++)
        ans += p[i];
    return ans;
}
int main()
{
  // freopen("in.txt", "r", stdin);
    int n;

    while(cin >> n >> t && n && t)
    {
        for(int i = 0; i <= n; i++)
            v[i].clear();
        for(int i = 1; i <= n; i++)
        {
            int a;
            cin >> a;
            v[a].push_back(i);
        }
        cout << dp(0) << endl;
    }
    return 0;
}

你可能感兴趣的:(UVA12186 - Another Crisis)