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简单贪心--活动安排问题
活动安排问题就是要在所给的活动集合中选出最大的相容活动子集合,是可以用贪心算法有效求解的很好例子.该问题要求高效地安排一系列争用某一公共资源的活动.贪心算法提供了一个简单.漂亮的方法使得尽可能多的活动 ...
随机推荐
- 【Hadoop】Apache Eagle 简介--分布式实时 Hadoop 数据安全方案
eBay公司隆重宣布正式向开源业界推出分布式实时安全监控引方案 - Apache Eagle ,该项目已正式加入Apache 称为孵化器项目.Apache Eagle提供一套高效分布式的流式策略引擎, ...
- CKEditor+SWFUpload实现功能较为强大的编辑器(一)---CKEditor配置
CKEditor爆表的强大功能大家都有目共睹,号称最强大的在线编辑器,只要将文件复制到项目中,在添加引用,在一句代码就可以将普通的textarea变成华丽的编辑器 所谓一复制,一拖,一换就大功告成 但 ...
- #淘宝#复制分享宝贝内容,打开淘宝APP,自己主动弹出宝贝提示信息
场景描写叙述: 淘宝复制连接,分享出去,比方拷贝到QQ好友.微信好友,一个宝贝信息.然后你朋友长按复制你分享它了的这个宝贝.当然打开手机淘宝时.自己主动会跳出宝贝的信息,点击确定能够直接进入宝贝详情 ...
- 虚拟机只有IPv6,没有ipv4
1.修改配置文件 vi /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 BOOTPROTO=none HWADDR=00:0C:29:20: ...
- Nginx:管理HTTP模块的配置项
参考资料<深入理解Nginx> 一个nginx.conf的例子 http { mytest_num ; server { server_name A; listen ; mytest_nu ...
- 【BIEE】02_新建资料库并创建简单分析
一.新建资料库 1.开始→打开BI管理→点击新建资料库 2.文件→新建资料档案库 下一步 在下面的框中一次填入 连接类型:OCI 10g/11g(直接选择即可) 数据库名称:(DESCRIPTION ...
- 自制的React同构脚手架
代码地址如下:http://www.demodashi.com/demo/12575.html Web前端世界日新月异变化太快,为了让自己跟上节奏不掉队,总结出了自己的一套React脚手架,方便日后新 ...
- Docker Push 镜像到公共仓库
首选需要在https://hub.docker.com/上注册用户. 1.登录docker账号主要命令:docker login sudo docker login 2.推送镜像主要命令:docker ...
- Android文章收藏
Android集 1.Himi李华明的<Android游戏开发专栏>http://blog.csdn.net/column/details/androidgame.html2.老罗的&l ...
- Android适配方案小结(二)
该节主要记录从代码中获取与屏幕适配相关的各个參数: Java代码例如以下 public class ScreenUtil { /** * Note: * 仅仅有activity能够使用getWindo ...