首页 > 代码库 > ZOJ 2315 New Year Bonus Grant

ZOJ 2315 New Year Bonus Grant

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

Andrew Stankevich‘s Contest #1
 
解题:贪心
 
 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib>10 #include <string>11 #include <set>12 #include <stack>13 #define LL long long14 #define pii pair<int,int>15 #define INF 0x3f3f3f3f16 using namespace std;17 const int maxn = 500010;18 struct arc{19     int to,next;20     arc(int x = 0,int y = -1){21         to = x;22         next = y;23     }24 };25 struct node{26     int p,v;27     node(int x = 0,int y = 0){28         p = x;29         v = y;30     }31 };32 arc e[maxn<<1];33 int head[maxn],tot,hd,tl;34 bool vis[maxn];35 void add(int u,int v){36     e[tot] = arc(v,head[u]);37     head[u] = tot++;38     e[tot] = arc(u,head[v]);39     head[v] = tot++;40 }41 node q[maxn];42 void bfs(){43     hd = tl = 0;44     memset(vis,false,sizeof(vis));45     q[tl++] = node(0,1);46     vis[1] = true;47     while(hd < tl){48         node now = q[hd++];49         for(int i = head[now.v]; ~i; i = e[i].next){50             if(vis[e[i].to]) continue;51             vis[e[i].to] = true;52             q[tl++] = node(now.v,e[i].to);53         }54     }55 }56 int sel[maxn];57 int main() {58     int t,n,u,ans,cnt;59     scanf("%d",&t);60     while(t--){61         scanf("%d",&n);62         memset(head,-1,sizeof(head));63         cnt = ans = tot = 0;64         for(int i = 1; i < n; i++){65             scanf("%d",&u);66             add(u,i+1);67         }68         bfs();69         memset(vis,false,sizeof(vis));70         for(int i = tl-1; i; i--){71             if(!vis[q[i].v] && !vis[q[i].p]){72                 ans++;73                 vis[q[i].v] = vis[q[i].p] = true;74                 sel[cnt++] = q[i].v;75             }76         }77         printf("%d\n",ans*1000);78         sort(sel,sel+cnt);79         for(int i = 0; i+1 < cnt; i++)80             printf("%d ",sel[i]);81         printf("%d\n",sel[cnt-1]);82     }83     return 0;84 }
View Code

 

ZOJ 2315 New Year Bonus Grant