题意:员工之间形成一棵树,上级可以给下级发奖金,任何一个人最多可以给一个下级发,并且发了奖金后就不能接受奖金。求总共最多可以产生多少的奖金流动

思路:每次选择没有下级并且有上级的员工a,令它的上级为b,那么让b给a发奖金,之后把a和b从树中删掉,这样处理直到不存在这样的员工a。也就是说每次让叶子员工接受奖金。简单证明:对于最优情况,叶子和它的兄弟集合还有它的父亲一定有一个接受了奖金,否则可以选叶子接受从父亲发的奖金,这样比原来增加了1个奖金;假设父亲接受了奖金或者兄弟接受了奖金,那么换成自己接收奖金,奖金数目不会变少,并且一定合法。所以这样选一定是最优的。

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#pragma comment(linker, "/STACK:10240000")
#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; #define X first
#define Y second
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define fillchar(a, x) memset(a, x, sizeof(a))
#define fillarray(a, b) memcpy(a, b, sizeof(a)) typedef long long ll;
typedef pair<int, int> pii;
typedef unsigned long long ull; #ifndef ONLINE_JUDGE
namespace Debug {
void RI(vector<int>&a,int n){a.resize(n);for(int i=;i<n;i++)scanf("%d",&a[i]);}
void RI(){}void RI(int&X){scanf("%d",&X);}template<typename...R>
void RI(int&f,R&...r){RI(f);RI(r...);}void RI(int*p,int*q){int d=p<q?:-;
while(p!=q){scanf("%d",p);p+=d;}}void print(){cout<<endl;}template<typename T>
void print(const T t){cout<<t<<endl;}template<typename F,typename...R>
void print(const F f,const R...r){cout<<f<<", ";print(r...);}template<typename T>
void print(T*p, T*q){int d=p<q?:-;while(p!=q){cout<<*p<<", ";p+=d;}cout<<endl;}
}
#endif // ONLINE_JUDGE template<typename T>bool umax(T&a, const T&b){return b<=a?false:(a=b,true);}
template<typename T>bool umin(T&a, const T&b){return b>=a?false:(a=b,true);} const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const double EPS = 1e-8; /* -------------------------------------------------------------------------------- */ const int maxn = 5e5 + ; int son[maxn], fa[maxn]; int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#endif // ONLINE_JUDGE
int n, x;
while (cin >> n) {
for (int i = ; i < n; i ++) {
scanf("%d", &x);
fa[i + ] = x;
son[x] ++;
}
queue<int> Q;
for (int i = ; i <= n; i ++) {
if (son[i] == ) Q.push(i);
}
vector<int> ans;
while (!Q.empty()) {
int H = Q.front(); Q.pop();
if (son[fa[H]] > ) {
ans.pb(H);
son[fa[H]] = ;
if (-- son[fa[fa[H]]] == ) Q.push(fa[fa[H]]);
}
}
cout << * ans.size() << endl;
sort(all(ans));
for (int i = ; i < ans.size(); i ++) {
printf("%d%c", ans[i], i == ans.size() - ? '\n' : ' ');
}
}
return ;
}

[ACdream 1212 New Year Bonus Grant]贪心的更多相关文章

  1. sgu 195 New Year Bonus Grant【简单贪心】

    链接: http://acm.sgu.ru/problem.php?contest=0&problem=195 http://acm.hust.edu.cn/vjudge/contest/vi ...

  2. ZOJ 2315 New Year Bonus Grant

    New Year Bonus Grant Time Limit: 5000ms Memory Limit: 32768KB This problem will be judged on ZJU. Or ...

  3. SGU 195. New Year Bonus Grant

    时间限制:0.75s 空间限制:4M 题意: 在一颗树(最多500000个节点)中,可以对节点染色,但是一个节点染了色后,它的父节点和兄弟节点都不能再染了,求最大的染色节点数,并输出所有染色节点. S ...

  4. zoj 2315 New Year Bonus Grant 解题报告

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1315 题目意思:Bill Hates 是公司的老总,她管辖着很多程序 ...

  5. SGU 分类

    http://acm.sgu.ru/problemset.php?contest=0&volume=1 101 Domino 欧拉路 102 Coprime 枚举/数学方法 103 Traff ...

  6. ASC #1

    开始套题训练,第一套ASC题目,记住不放过每一题,多独立思考. Problem A ZOJ 2313 Chinese Girls' Amusement 循环节 题意:给定n,为圆环长度,求k < ...

  7. [置顶] 2013_CSUST暑假训练总结

    2013-7-19 shu 新生训练赛:母函数[转换成了背包做的] shuacm 题目:http://acm.hdu.edu.cn/diy/contest_show.php?cid=20083总结:h ...

  8. DP:0

    小故事: A * "1+1+1+1+1+1+1+1 =?" * A : "上面等式的值是多少" B : *计算* "8!" A *在上面等式 ...

  9. ACdream 1224 Robbers (贪心)

    一道贪心题,很久前做的,代码是我以前写的. 题意:有n个抢劫者抢劫了m块金子,然后第i个人平分xi/y块金子,但是会有除不尽的情况而金子不可再分,那么每个人都有一个不满意度fabs(xi / y - ...

随机推荐

  1. Python - 调用接口合并文件夹下多个Excel表

    在工作中经常遇到需要打开许多个excel表格,然后合并的需求,合并的同时要求格式必须原汁原味的保留.利用VBA代码可以比较轻松的解决,现在我们来看Python中如何实现. 上代码: from open ...

  2. mysql 创建表 索引 主键 引擎 自增 注释 编码等

    CREATE TABLE text(id INT(20) COMMENT '主键',NAME VARCHAR(20) COMMENT '姓名',PASSWORD VARCHAR(20) COMMENT ...

  3. sqli-labs通关教程----41~50关

    第四十关 与前几关一样,闭合变成') 插入数据 ?id=1') ;insert into users(id,username,password) values('17','aaa','bbb'); % ...

  4. 关于Google下插件SwitchyOmega用法

    开启代理后,尽管访问很自由了,但是我的搬瓦工,是有流量限制的.所以,在之前,我开启一会自由访问模式(戏称),然后关一会,用来方便打开国内网站. 是的,我这么坚持了半个月,之后,就崩溃了,太尼玛繁琐了! ...

  5. 微信自动关闭内置浏览器页面,返回公众号窗口 WeixinJSBridge.call('closeWindow')

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  6. 2019-2020-1 20199325《Linux内核原理与分析》第一周作业

    1.显示一句话welcome !/bin/bash script4-1.sht var1="welcome to use Shell script" echo $var1 pwd ...

  7. [Inno Setup] How to create a OuterNotebook/welcome page in the uninstaller

    By Martin Prikryl https://stackoverflow.com/questions/42625626/inno-setup-how-to-create-a-outernoteb ...

  8. Java_Web--JDBC 增加记录操作模板

    如果不能成功链接数据库,我的博客JAVA中有详细的介绍,可以看一下 import java.sql.Connection; import java.sql.DriverManager; import ...

  9. 图论--割点--Tarjan模板

    #include <iostream> #include <algorithm> #include <cstdio> #include <cstring> ...

  10. python-函数变量与方法公有、私有整理

    关于函数与方法的区别: 函数: def funname(): 括号里面可以有形参,也可以没有,为空 在函数里面的参数整理: 方法: def funcname(self): 括号里面必须有个self,因 ...