UVA - 12186 Another Crisis(工人的请愿书)(树形dp)
2024-10-08 17:02:26
题意:某公司有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;
}
最新文章
- 大端小端系统_union_栈的增长方向
- matlab练习程序(构造简单多边形)
- android 学习随笔十一(网络:HttpClient框架)
- SQL SERVER 2008函数大全(含例子)
- Jquery attr判断服务器单选按钮失败
- linq 和 , 并 , 差 ,交
- C# 每天温习一点(IEnumerable<;TSource>;)
- oracle删除表语句
- Oracle SQL tuning 步骤
- LSJ_NHibernate第三章 IDAL,DAL,BLL
- JavaScript修改表中的内容
- Android之旅十八 百度地图环境搭建
- SHDP--Working With HBase (二)之HBase JDBC驱动Phoenix与SpringJDBCTemplate的集成
- [置顶] 自己动手写Web容器之TomJetty之六:动态页面引入
- J2EE之验证码实现
- 程序媛也话Android 之 自定义控件(垂直方向滑动条)
- jst通用删除数组中重复的值和删除字符串中重复的字符
- Java WebService学习资料
- 修改Linux命令提示符
- 缓存设计(cache-design)