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简单贪心--活动安排问题
活动安排问题就是要在所给的活动集合中选出最大的相容活动子集合,是可以用贪心算法有效求解的很好例子.该问题要求高效地安排一系列争用某一公共资源的活动.贪心算法提供了一个简单.漂亮的方法使得尽可能多的活动 ...
随机推荐
- 2017.3.31 spring mvc教程(六)转发、重定向、ajax请求
学习的博客:http://elf8848.iteye.com/blog/875830/ 我项目中所用的版本:4.2.0.博客的时间比较早,11年的,学习的是Spring3 MVC.不知道版本上有没有变 ...
- running android lint has encountered a
近期写学习android编程的的时候,每次保存.java文件的时候,总会跳出例如以下错误 这个错误不是属于程序错误,把它关掉对于编程没有不论什么影响,但每次见到这个就是不爽,希望大神可以解决一下,谢谢 ...
- Angular 学习笔记——factory
<!DOCTYPE HTML> <html ng-app="myApp"> <head> <meta http-equiv="C ...
- Angular 学习笔记——sublime
div.row>div.col-md-12*10 就等于 <div class="row"> <div class="col-md-12&q ...
- Quaternion 四元数
Quaternions are used to represent rotations. 四元数用于表示旋转. They are compact, don't suffer from gimbal l ...
- 【BIEE】由于排序顺序不兼容,集合操作失败
问题描述 使用BIEE数据透视表时,使用了UNION进行数据组合,但是在浏览结果时意外出错了,报错如下截图: 问题分析 原因暂时未知 问题解决 目前使用UNION进行聚合,只需要将UNION修改为UN ...
- TabControl
1. ItemsSource="{Binding GroupList}" SelectedItem="{Binding SelectedGroupItem,Mode=Tw ...
- SQL数据库从高版本到低版本的迁移,同时解决sql脚本文件太大无法打开的尴尬问题
as we known,sql数据库高版本向低版本还原是不太可能但是又经常会碰到的事,今天实测了一种方法 步骤:任务—>生成脚本—> 下一步->高级,选择数据库版本和编写脚本数据类型 ...
- cocos2dx3.0戳青蛙游戏(打地鼠)
1戳青蛙项目描写叙述 1.1功能描写叙述 实现类似打地鼠游戏.青蛙随机出如今屏幕左边5*3的格子中,并会向屏幕右边移动,在青蛙逃离之前,手指点击实现戳灭青蛙的效果.随着分数添加,青蛙越来越多,当青蛙逃 ...
- Failed to read artifact descriptor for org.apache.httpcomponents:httpmime:jar
额,在Stackoverflow上找到了一个答案: I had this in eclipse and did this which fixed it(even though my command l ...