New Year Bonus Grant

Time Limit: 5000ms
Memory Limit: 32768KB

This problem will be judged on ZJU. Original ID: 2315
64-bit integer IO format: %lld      Java class name: Main

Special Judge
 
 

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

 

Source

 
解题:贪心
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
struct arc{
int to,next;
arc(int x = ,int y = -){
to = x;
next = y;
}
};
struct node{
int p,v;
node(int x = ,int y = ){
p = x;
v = y;
}
};
arc e[maxn<<];
int head[maxn],tot,hd,tl;
bool vis[maxn];
void add(int u,int v){
e[tot] = arc(v,head[u]);
head[u] = tot++;
e[tot] = arc(u,head[v]);
head[v] = tot++;
}
node q[maxn];
void bfs(){
hd = tl = ;
memset(vis,false,sizeof(vis));
q[tl++] = node(,);
vis[] = true;
while(hd < tl){
node now = q[hd++];
for(int i = head[now.v]; ~i; i = e[i].next){
if(vis[e[i].to]) continue;
vis[e[i].to] = true;
q[tl++] = node(now.v,e[i].to);
}
}
}
int sel[maxn];
int main() {
int t,n,u,ans,cnt;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
memset(head,-,sizeof(head));
cnt = ans = tot = ;
for(int i = ; i < n; i++){
scanf("%d",&u);
add(u,i+);
}
bfs();
memset(vis,false,sizeof(vis));
for(int i = tl-; i; i--){
if(!vis[q[i].v] && !vis[q[i].p]){
ans++;
vis[q[i].v] = vis[q[i].p] = true;
sel[cnt++] = q[i].v;
}
}
printf("%d\n",ans*);
sort(sel,sel+cnt);
for(int i = ; i+ < cnt; i++)
printf("%d ",sel[i]);
printf("%d\n",sel[cnt-]);
}
return ;
}

ZOJ 2315 New Year Bonus Grant的更多相关文章

  1. zoj 2315 New Year Bonus Grant 解题报告

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1315 题目意思:Bill Hates 是公司的老总,她管辖着很多程序 ...

  2. sgu 195 New Year Bonus Grant【简单贪心】

    链接: http://acm.sgu.ru/problem.php?contest=0&problem=195 http://acm.hust.edu.cn/vjudge/contest/vi ...

  3. ZOJ 2315

    ---恢复内容开始--- http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1315 这个题目比较难以看懂,我也是看网上的题目意思才 ...

  4. ZOJ 1642 Match for Bonus (DP)

    题目链接 题意 : 给你两个字符串,两个字符串都有共同的字母,给你每个字母的值,规则是,找出两个字符串中的共同的一个字母,然后这个字母的值就可以加到自己的分数上,但是这步操作之后,这两个字母及其之前的 ...

  5. SGU 195. New Year Bonus Grant

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

  6. 贪心,二叉树搜索,ZOJ(2315)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1315 解题报告: #include <stdio.h> ...

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

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

  8. ASC #1

    开始套题训练,第一套ASC题目,记住不放过每一题,多独立思考. Problem A ZOJ 2313 Chinese Girls' Amusement 循环节 题意:给定n,为圆环长度,求k < ...

  9. [置顶] 2013_CSUST暑假训练总结

    2013-7-19 shu 新生训练赛:母函数[转换成了背包做的] shuacm 题目:http://acm.hdu.edu.cn/diy/contest_show.php?cid=20083总结:h ...

随机推荐

  1. fputs与fgets

    1.      fputs 函数名: fputs  功  能: 送一个字符到一个流中  用  法: int fputs(char *string, FILE *stream); 说明: fputs是一 ...

  2. S 禁止F12和右键操作控制台,兼容各浏览器

    document.oncontextmenu = function () { return false; };         document.onkeydown = function () {   ...

  3. Windows XP时代终结:假设你还在用它怎么办

    2014 年 4 月 8 日.是Windows XP退休的日子. 4 月 8 日过后,Windows XP 的安全性更新将停止了. watermark/2/text/aHR0cDovL2Jsb2cuY ...

  4. 用python阐释工作量证明(proof of work)

    了解比特币的都知道挖矿非常耗电,这是由于比特币用到了工作量证明. 工作量证明是指系统为达到某目标而设置的工作度量方法.一開始是用在网络攻防上,大大提高攻击者的计算量,攻击成本也就上去了. 工作量证明须 ...

  5. 常用框架(一):spring+springMvc+mybatis+maven

    项目说明: (1) 本例采用 maven web 工程做例子讲解 (2) 利用mybaits 提供的代码生成工具自动生成代码(dao接口,sql mapper映射文件,pojo数据库映射类) (3) ...

  6. number随时间随机递增每天 不同 php(原创)

    <?php namespace Controller; class IndexController{ public function getAutoRegUserName(){ $sql=&qu ...

  7. SMTP协议详解

    简单邮件传输协议 (Simple Mail Transfer Protocol, SMTP) 是在Internet传输email的事实标准. SMTP是一个相对简单的基于文本的协议.在其之上指定了一条 ...

  8. Java多线程编程那些事:volatile解惑--转

    http://www.infoq.com/cn/articles/java-multi-thread-volatile/ 1. 前言 volatile关键字可能是Java开发人员“熟悉而又陌生”的一个 ...

  9. 原生js实现简单JSONP

    JSONP是一种非常常见的实现跨域请求的方法.其基本思想是利用浏览器中可以跨域请求外链的JS文件,利用这一特性实现数据传输. 用原生JS实现JSONP非常简单,无非几点: 1)定义一个函数,用于处理接 ...

  10. poj3281 Dining 最大流(奇妙的构图)

    我是按照图论500题的文档来刷题的,看了这题怎么也不觉得这是最大流的题目.这应该是题目做得太少的缘故. 什么是最大流问题?最大流有什么特点? 最大流的特点我觉得有一下几点: 1.只有一个起点.一个终点 ...