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简单贪心--活动安排问题
活动安排问题就是要在所给的活动集合中选出最大的相容活动子集合,是可以用贪心算法有效求解的很好例子.该问题要求高效地安排一系列争用某一公共资源的活动.贪心算法提供了一个简单.漂亮的方法使得尽可能多的活动 ...
随机推荐
- Python 把u'\xca\xd3\xc6\xb5\xd7\xa5\xc8\xa1' 输出正常中文
今天碰见从数据库读取出来数据是u'\xca\xd3\xc6\xb5\xd7\xa5\xc8\xa1',输出显示乱码,经常查询处理如下: 两种方式: 1. s = u'\xca\xd3\xc6\xb5\ ...
- CentOS7下挂载硬盘笔记
CentOS7下挂载硬盘笔记 准备工作 机器:DELL R730 系统:CentOS 7.4.1708 (Core) x86_64 新增硬盘:三星960PRO 关闭服务器加上新硬盘,然后重启 查看硬盘 ...
- react-native 组件默认属性(defaultProps) 及 属性类型验证(PropTypes)
1.所有的属性类型 2.代码 import PropTypes from 'prop-types'; type Props = {}; export default class App extends ...
- 51单片机 | 实现SMG12864液晶显示器操作
———————————————————————————————————————————— SMG12864液晶显示器 128*64个点位,可以显示图形或8*4个汉字 - - - - - - - - - ...
- 超高逼格Log日志打印
代码地址如下:http://www.demodashi.com/demo/12646.html 前言 Log日志的打印一直是一个比较头疼的事,怎样才能让自己的log显示更多信息,怎样才能让自己的log ...
- 通用对话弹窗CommonDialog
代码地址如下:http://www.demodashi.com/demo/12592.html 通用对话弹窗CommonDialog Version 1.0 Created by chenchangj ...
- 基于React的贪吃蛇游戏的设计与实现
代码地址如下:http://www.demodashi.com/demo/11818.html 贪吃蛇小游戏(第二版) 一年半前层用react写过贪吃蛇小游戏https://github.com/ca ...
- C# 指南之装箱与拆箱
基础 1.值类型 1.1 在栈上分配内存,在声明时初始化才能使用,不能为null. 1.2 值类型超出作用范围系统自动释放内存. 1.3 主要由两类组成:结构,枚举 结构分为以下几类 1.整形(Sby ...
- 工作总结 npoi 模板 导出公式 excel
Apache POI(5):公式(formula) Apache POI(5):公式(formula) 2016年08月01日 17:44:49 阅读数:1145 package com.hthk ...
- 搭建Squid反向代理服务器
好吧,更新个文章,有段时间没写技术博文了.今天就说说squid反向代理这个服务,当然,这是在Linux下配置完成的.说自己没偏见似乎不可能 了.大概是相对喜欢Linux而已.但我从不否认Windows ...