链接:



195. New Year Bonus Grant

time limit per test: 1.5 sec.

memory limit per test: 65536 KB
input: standard

output: standard
All programmers of Mocrosoft software company are organized in a strict subordination hierarchy. Every programmer has exactly one chief, except Bill Hates who is also the head of the company and has no chief. 



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

4 1 1 2 
Output

2000 

3 4 


昨晚比赛快完的时候才看懂题意,果断也没有写出来了,刚刚翻了下 cxb 去年的PPT(当时居然没有看懂
就大致 copy 了下思路和题意了。cxb 童鞋就这么放弃了实在可惜,不过人各有志了Orz

题意:

•Mocrosoft software这个公司有N个员工。除了老板(BillHates)以外,其他每个人都有一个自己的上司。现在过年了,老板打算给员工们发奖金。为了让支出最少,现在有三个规则:

·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【简单贪心】的更多相关文章

  1. SGU 195. New Year Bonus Grant

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

  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. CF 628C --- Bear and String Distance --- 简单贪心

    CF 628C 题目大意:给定一个长度为n(n < 10^5)的只含小写字母的字符串,以及一个数d,定义字符的dis--dis(ch1, ch2)为两个字符之差, 两个串的dis为各个位置上字符 ...

  4. Uva 11729 Commando War (简单贪心)

    Uva 11729  Commando War (简单贪心) There is a war and it doesn't look very promising for your country. N ...

  5. CDOJ 1502 string(简单贪心)

    题目大意:原题链接 相邻两个字母如果不同,则可以结合为前一个字母,如ac可结合为a.现给定一个字符串,问结合后最短可以剩下多少个字符串 解体思路:简单贪心 一开始读题时,就联想到之前做过的一道题,从后 ...

  6. ACM_发工资(简单贪心)

    发工资咯: Time Limit: 2000/1000ms (Java/Others) Problem Description: 作为广财大的老师,最盼望的日子就是每月的8号了,因为这一天是发工资的日 ...

  7. ACM_Ruin of Titanic(简单贪心)

    Ruin of Titanic Time Limit: 2000/1000ms (Java/Others) Problem Description: 看完Titanic后,小G做了一个梦.梦见当泰坦尼 ...

  8. [ACdream 1212 New Year Bonus Grant]贪心

    题意:员工之间形成一棵树,上级可以给下级发奖金,任何一个人最多可以给一个下级发,并且发了奖金后就不能接受奖金.求总共最多可以产生多少的奖金流动 思路:每次选择没有下级并且有上级的员工a,令它的上级为b ...

  9. hdu 2037简单贪心--活动安排问题

    活动安排问题就是要在所给的活动集合中选出最大的相容活动子集合,是可以用贪心算法有效求解的很好例子.该问题要求高效地安排一系列争用某一公共资源的活动.贪心算法提供了一个简单.漂亮的方法使得尽可能多的活动 ...

随机推荐

  1. 使用springMVC上传文件

    control层实现功能: @RequestMapping(value="upload2") public String upLoad2(HttpServletRequest re ...

  2. Commenting and uncommenting XML via Python

    转载: http://stackoverflow.com/questions/8764017/commenting-and-uncommenting-xml-via-python from xml.d ...

  3. tomcat修改默认访问首页

    找到conf下server.xml文件修改如下位置内容 <Host name="localhost" appBase="webapps" unpackWA ...

  4. 转:提高ios通过率的注意点

    http://bugly.qq.com/bbs/forum.php?mod=viewthread&tid=431 Bugly 技术干货系列内容主要涉及移动开发方向,是由 Bugly 邀请腾讯内 ...

  5. JAVA Eclipse如何导出APK程序

    为了最快的导出程序,直接导出未签名的APK   但是大部分手机不允许安装未经签名的程序   导出签名的项目   设置密码,后面的都可以随便设置   这样最后生成的APK就可以直接双击完成安装     ...

  6. SecureCRT如何调整好看的黄色

    1.常规 →默认会话→编辑默认编辑→白黑 字体为console 2.全局选项 ANSI颜色有一个 把黄色 拖过去即可

  7. JavaScript对象this指向(普通键this指向 非指向函数的键)

    1.结论 JavaScript对象普通键(非指向函数的键)this指向是window. 2.示例 <!DOCTYPE html> <html lang="zh"& ...

  8. 编辑mac系统环境变量后保存,提示没有权限用到下面这个命令

    编辑的文件是vim /etc/paths :w !sudo tee % %代表当前编辑文件名 MAC:查看端口占用情况: lsof -i tcp: list open files lsof -i 用以 ...

  9. Jenkins Docker 插件

    原文地址:https://wiki.jenkins.io/display/JENKINS/Docker+Plugin Created by magnayn -, last modified by Ni ...

  10. 零基础学python-1.5 第一个程序

    这一个章节我们来说说怎么建立一个python程序 1.打开idle 2.点击File->new file,然后会弹出一个编辑窗体 3.在编辑窗体里面输入命令代码 程序代码: print(&quo ...