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 ;
}

最新文章

  1. 我们常用,却容易忽视——CSS的BFC(Block formatting contexts)
  2. Oracle Flashback 闪回
  3. 想要上市,SaaS 企业应该重点关注什么?(下)
  4. Prototype 原型模式
  5. Android中设置文本颜色的三种方法
  6. 被误解的 MVC 和被神化的 MVVM
  7. Eloquent ORM 之关联查询
  8. 基于visual Studio2013解决面试题之0808寻找中间数
  9. SQL声明大全
  10. java中的反编译
  11. Unbutu14.04 切换ROOT用户后无法启用音频
  12. 小程序1_app.json配置
  13. 基于Python的数据分析(1):配置安装环境
  14. 译MassTransit 快速入门
  15. Word中使用宏处理表格内容 小记
  16. P1182 数列分段`Section II`(贪心+二分, 好题)
  17. 第一周Access课总结
  18. Excel连接SSAS提示“传输层中遇到错误”的问题
  19. QGis C++ 开发之图层分类显示
  20. thnkphp框架面试问题

热门文章

  1. HDOJ GCD 2588【欧拉函数】
  2. 自己定义隐式转换和显式转换c#简单样例
  3. oc16--set,get
  4. 开源DDos 机器学习思路求解的一些源码——TODO 待分析
  5. oracle ash性能报告的使用方法
  6. thinkphp 内存查询表 防止多次查库
  7. git 本地项目推送至远程仓库
  8. Struts2的学习链接
  9. js判断浏览器是android还是ios还是微信浏览器
  10. Oracle数据库实例