ZOJ 2315 New Year Bonus Grant(贪心)

115 篇文章 0 订阅
31 篇文章 0 订阅

New Year Bonus Grant

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.

This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.

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 Input

1

4
1 1 2

Sample Output

2000
3 4


题意:微软公司要发一些奖金,有三种发奖金的规则:
1.每一个员工可以从他的上司那获得奖金,也可以发给它的下属一份奖金,或者什么也不做
2.每一个员工不能够同时接收奖金和发放奖金,也就是每一个员工只能操作一次
3.每一个上司最多只能够把奖金发给他的一个下属

问,公司的员工总的最多能够获得多少奖金

思路:因为每一个下属的编号都大于它上司的编号,所以可以直接从最大的编号开始遍历,如果当前节点和它的父节点都没有操作过,那就说明这个点可以获得奖金,然后同时标记和记录下这个节点和父节点,最后直至遍历完(具体详见代码)

代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

#define maxn 500010
int fa[maxn],vis[maxn],ans[maxn];
int n;

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(vis,0,sizeof(vis));
        scanf("%d",&n);
        for(int i=2;i<=n;++i)
            scanf("%d",&fa[i]);
        int ps=0;
        for(int i=n;i>=2;--i)//遍历
        {
            if(!vis[i]&&!vis[fa[i]])
            {
                vis[i]=vis[fa[i]]=1;
                ans[ps++]=i;
            }
        }
        sort(ans,ans+ps);
        printf("%d\n",ps*1000);
        for(int i=0;i<ps-1;++i)
            printf("%d ",ans[i]);
        printf("%d\n",ans[ps-1]);
        if(t)
            printf("\n");
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值