New Year Bonus Grant(SGU195,贪心or区间DP)

http://acm.sgu.ru/problem.php?contest=0&problem=195

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

1 1 2 

Output

2000 

3 4 

[submit]

[forum]

Author:Andrew Stankevich

Resource:Petrozavodsk Winter Trainings 2003

Date:2003-02-06

解析:

题意:一个公司给员工发奖金,N个员工编号为1——-N.每个员工除1号员工外都有自己的上司。

现发奖金规定是这样的:作为上司可以接受上级发的奖金,可以什么都不做,可以发奖金给直接下属。但这三种行为只能选一个,也就是说不能即受到奖金又发奖金给别人

思路:据说是树形Dp

我是用贪心法,

构造树,自底而顶遍历,如果自己的上级没有接受奖金(那么就能够把钱给发自己了),而自己也从不增接受奖金则可以获得奖金,同时标记自己和上级。这样下去就可以得到可得到最大奖金

注意:为什么从下级开始找呢、奖金总要有个归处,除了1其他都是下级,故如此。

目前是这样理解的

*/

#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
const int maxn=500000+10;
int tr[maxn],p[maxn];
int vis[maxn];
int main()
{
int n,i,t;
while(scanf("%d",&n)!=EOF)
{
memset(tr,0,sizeof(tr));
memset(vis,0,sizeof(vis));
memset(p,0,sizeof(p));
for(i=2;i<=n;i++)
scanf("%d",&tr[i]);
t=0;
for(i=n;i>1;i--)
if(!vis[tr[i]]&&!vis[i])//如果本身和上级没有接触过奖金则可以或的奖金
{
vis[i]=1;
vis[tr[i]]=1;//此时上司就不能获得奖金了
p[t++]=i;
}
printf("%d\n",1000*t);
for(i=t-1;i>=0;i--)
{  printf("%d",p[i]);
if(i!=0)
printf(" ");
else
printf("\n");
}
}
return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值