首页 > 代码库 > UVA - 12186 Another Crisis(工人的请愿书)(树形dp)

UVA - 12186 Another Crisis(工人的请愿书)(树形dp)

题意:某公司有1个老板和n(n<=105)个员工组成树状结构,除了老板之外每个员工都有唯一的直属上司。老板的编号为0,员工编号为1~n。无下属的员工(叶子)打算签署一项请愿书递给老板,但不能跨级递,只能递给直属上司。当一个中级员工(非叶子)的直属下属中不小于T%的人签字时,他也会签字并且递给他的直属上司。问:要让公司老板收到请愿书,至少需要多少个工人签字?

分析:

1、dfs(u)表示让u给上级发信最少需要多少个工人。

2、需要在u的孩子结点中选择不小于T%的人数,这些人所需的工人签字越少越好,所以需要排序。

#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
const double eps = 1e-8;
inline int dcmp(double a, double b){
    if(fabs(a - b) < eps) return 0;
    return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 1e5 + 10;
const int MAXT = 10000 + 10;
using namespace std;
vector<int> v[MAXN];
int N, T;
int dfs(int cur){
    int len = v[cur].size();
    if(!len) return 1;
    vector<int> tmp;
    for(int i = 0; i < len; ++i){
        tmp.push_back(dfs(v[cur][i]));
    }
    sort(tmp.begin(), tmp.end());
    int cnt = (int)ceil(len * (double)T / 100);
    int ans = 0;
    for(int i = 0; i < cnt; ++i){
        ans += tmp[i];
    }
    return ans;
}
int main(){
    while(scanf("%d%d", &N, &T) == 2){
        if(!N && !T) return 0;
        for(int i = 0; i < MAXN; ++i) v[i].clear();
        for(int i = 1; i <= N; ++i){
            int x;
            scanf("%d", &x);
            v[x].push_back(i);
        }
        printf("%d\n", dfs(0));
    }
    return 0;
}

  

UVA - 12186 Another Crisis(工人的请愿书)(树形dp)