sgu 195 New Year Bonus Grant【简单贪心】
链接:
195. New Year Bonus Grant
memory limit per test: 65536 KB
output: standard
Due to the celebration of the new 2003 year, chief accountant of Mocrosoft decided to pay a New Year Bonus Grant of 1000 dollars to some programmers. However being extremely concerned of the company wealth she would like to designate the least possible
amount of money for these grants. On the other hand she didn't want to be accused of being too greedy or of giving preferences to some programmers. To do this, she developed the following scheme of grants appointment:
- Each programmer may either assign a grant to one of his subordinates or have a grant assigned to him by his chief or none of the above.
- No programmer can simultaneously receive a grant and assign a grant to one of his subordinates.
- No programmer can assign a grant to more than one of his subordinates
The scheme seemed to be designed perfectly — nobody would like to assign a grant to anybody since in this case he himself would not receive money. But programmers somehow discovered the plan of chief accountant and decided to make a trick to get the most money
possible and share them fairly afterwards. The idea was to make such grant assignments that the total amount of grant money received is maximum possible.
You were selected to write the program which will find the optimal grants appointment.
Input
The first line of the input file contains integer N — the number of programmers in Mocrosoft company (2 ≤ N ≤ 500 000). Each programmer is assigned his unique identifier — integer number ranging from 1 to N. Bill Hates has number 1 and each programmer
has the number greater then the number of his chief. The second line of the input file contains N-1 integers, i-th of which being the number of the chief of the worker whose number is (i + 1).
Output
On the first line of the output file print the maximum possible amount of money workers can get. On the second line output the numbers of programmers that will receive grant in ascending order.
Sample test(s)
Input
3 4
)题意:
·Eachprogrammer may either assign a grant to one of his subordinates or have a grantassigned to him by his chief or none of the above.
一、每个员工可以安排自己的下属拿奖金,可以等待拿自己上司给自己的奖金。也可以什么都不做。(就是说
他给下属安排奖金后,他就不能有奖金了)
·Noprogrammer can simultaneously receive a grant and assign a grant to one of hissubordinates.
二、没有哪一个程序猿可以同时接收上司给的奖金,还给自己下属安排奖金。
(就是说他给下属安排奖金后,他就不能由奖金了!)
·Noprogrammer can assign a grant to more than one of his subordinates
三、每个程序猿最多只能给自己的一个下属(要是他有下属的话)安排奖金。
注意:每次输入的数是下一个点的父亲的编号,所以题中的输入就是一颗从上往下的树了,编写程序时从下往 上找即可。
算法:
思路:
问题转化为给一颗树染色:
(1)每一个节点至多只有一个儿子被染色
(2)如果某个节点被染色,那么它的所有儿子都不能染色
(也就是一个节点如果染色了,他的父亲,兄弟,儿子都不能染色)
code:
| Accepted | 6695 KB | 234 ms | Visual Studio C++ 2010 | 763 B |
#include<stdio.h>
#include<string.h> const int maxn = 500000+10; int p[maxn]; // 记录父亲
int vis[maxn]; // 标记是否分到钱
int ans[maxn]; // 记录分到钱的员工编号 int main()
{
int n;
while(scanf("%d", &n) != EOF)
{
int sum = 0;
for(int i = 2; i <= n; i++)
{
scanf("%d", &p[i]);
} memset(vis, 0, sizeof(vis)); // 初始化
for(int i = n; i > 1; i--) // 从最底层的节点开始找
{
if(!vis[i] && !vis[p[i]]) //如果自己没有分到钱,而且父亲和兄弟也没有分到钱
{
vis[i] = 1;
vis[p[i]] = 1;
ans[sum++] = i; // 分钱
}
}
printf("%d\n", sum*1000);
for(int i = sum-1; i >= 0; i--)
{
if(i == (sum-1)) printf("%d", ans[i]);
else printf(" %d", ans[i]);
}
printf("\n");
}
return 0;
}
sgu 195 New Year Bonus Grant【简单贪心】的更多相关文章
- SGU 195. New Year Bonus Grant
时间限制:0.75s 空间限制:4M 题意: 在一颗树(最多500000个节点)中,可以对节点染色,但是一个节点染了色后,它的父节点和兄弟节点都不能再染了,求最大的染色节点数,并输出所有染色节点. S ...
- ZOJ 2315 New Year Bonus Grant
New Year Bonus Grant Time Limit: 5000ms Memory Limit: 32768KB This problem will be judged on ZJU. Or ...
- CF 628C --- Bear and String Distance --- 简单贪心
CF 628C 题目大意:给定一个长度为n(n < 10^5)的只含小写字母的字符串,以及一个数d,定义字符的dis--dis(ch1, ch2)为两个字符之差, 两个串的dis为各个位置上字符 ...
- Uva 11729 Commando War (简单贪心)
Uva 11729 Commando War (简单贪心) There is a war and it doesn't look very promising for your country. N ...
- CDOJ 1502 string(简单贪心)
题目大意:原题链接 相邻两个字母如果不同,则可以结合为前一个字母,如ac可结合为a.现给定一个字符串,问结合后最短可以剩下多少个字符串 解体思路:简单贪心 一开始读题时,就联想到之前做过的一道题,从后 ...
- ACM_发工资(简单贪心)
发工资咯: Time Limit: 2000/1000ms (Java/Others) Problem Description: 作为广财大的老师,最盼望的日子就是每月的8号了,因为这一天是发工资的日 ...
- ACM_Ruin of Titanic(简单贪心)
Ruin of Titanic Time Limit: 2000/1000ms (Java/Others) Problem Description: 看完Titanic后,小G做了一个梦.梦见当泰坦尼 ...
- [ACdream 1212 New Year Bonus Grant]贪心
题意:员工之间形成一棵树,上级可以给下级发奖金,任何一个人最多可以给一个下级发,并且发了奖金后就不能接受奖金.求总共最多可以产生多少的奖金流动 思路:每次选择没有下级并且有上级的员工a,令它的上级为b ...
- hdu 2037简单贪心--活动安排问题
活动安排问题就是要在所给的活动集合中选出最大的相容活动子集合,是可以用贪心算法有效求解的很好例子.该问题要求高效地安排一系列争用某一公共资源的活动.贪心算法提供了一个简单.漂亮的方法使得尽可能多的活动 ...
随机推荐
- linux bash 入门
#!/bin/bash #shell使用的熟练成都反映用户对Unix/Linux使用的熟练程度 #shell 有两种执行命令的方式:交互式和批处理 #常见的shell脚本解释器有bash,sh,csh ...
- 基于Redis的Bloomfilter去重(转载)
转载:http://blog.csdn.net/bone_ace/article/details/53107018 前言 “去重”是日常工作中会经常用到的一项技能,在爬虫领域更是常用,并且规模一般都比 ...
- 转: Servlet 工作原理解析 from ibm
评点: 比较深入的讲了servlet容器, 作者许令波 (这个文章好像来自他自己的书中java web...) https://www.ibm.com/developerworks/cn/java/j ...
- 如何看一个VC工程具体是什么工程?
VC6等可以创建MFC, Win32等工程,拿到一个工程,怎么判断是什么工程呢? VC6全文检索看看有没有theApp 如果有一般就是MFC的 (VS?)看看工程设置,入口点函数写的是什么,/subs ...
- Elasticsearch Java API深入详解
0.题记 之前Elasticsearch的应用比较多,但大多集中在关系型.非关系型数据库与Elasticsearch之间的同步.以上内容完成了Elasticsearch所需要的基础数据量的供给.但想要 ...
- mac os x+paralles使用source insight
将Mac OS X下的目录共享到Paralles后,source insight创建工程.但是当再次打开时却打开失败.提示:there was an error opening project 网上对 ...
- 倒计时:CountDownLatch(火箭发射前的准备)读书笔记
这是一个非常实用的多线程控制工具类,经典的场景就是 火箭发射,在火箭发射前,为了保证万无一失,往往还要进行各项设备,仪器的检查,只有等待所有的检查完毕后,引擎才能点火, CountDown ...
- spring 国际化-i18n
i18n(其 来源是英文单词 internationalization的首末字符i和n,18为中间的字符数)是“国际化”的简称.在资讯领域,国际化(i18n)指让产品(出版 物,软件,硬件等)无需做大 ...
- Mysql视图的创建及使用
视图理解: 视图又叫虚表.同真实的表一样,视图包含一系列带有名称的列和行数据.但是,视图并不在数据库中以存储的数据值集形式存在.行和列数据来自由定义视图的查询所引用的表,并且在引用视图时动态生成. 视 ...
- (七)Thymeleaf的 th:* 属性之—— th: ->设值& 遍历迭代& 条件判断
3.4 属性值的设置 3.4.1 使用th:attr来设置属性的值 <form action="subscribe.html" th:attr="action=@{ ...