-
Notifications
You must be signed in to change notification settings - Fork 4
/
195.cpp
71 lines (67 loc) · 1.86 KB
/
195.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#define rep(i,j,k) for (int i=j;i<=k;++i)
#define rrep(i,j,k) for (int i=j;i>=k;--i)
#define PB(i) push_back(i)
#define vInt vector<int>
using namespace std;
struct childList{childList *next;int pos;}buf[500002];
struct treeNode{treeNode():child(0),withoutTo(0){}int without,with,withoutTo;childList *child;}*g[500002],buffer[500002];
int top,n,k,cnt,t,pos;
vInt ans;
void insert(int u,int v)
{
if (g[u] == 0) g[u] = &buffer[top++];
childList * x = &buf[cnt++];
x->pos = v,x->next = g[u]->child,g[u]->child = x;
}
void theIniter()
{
cin >> n;memset(g,0,sizeof(g));
rep(i,2,n) cin >> k,insert(k,i);
}
void dfsI(int u)
{
childList *y;treeNode *x = g[u];
if (x==0) {g[u] = &buffer[top++],g[u]->child = 0,g[u]->with=1,g[u]->without = g[u]->withoutTo = 0;return;}
for (y=x->child;y;y=y->next) dfsI(y->pos);
int sum=0,maxi=0;
for (y=x->child;y;y=y->next) sum += g[y->pos]->without;g[u]->with = sum+1;
for (y=x->child;y;y=y->next)
{
maxi = max(maxi,t=(sum-g[y->pos]->without+g[y->pos]->with));
if (t==maxi) pos = y->pos;
}g[u]->without = maxi,g[u]->withoutTo = pos;
return;
}
void dfsII(int u,int mode)
{
childList *y;
if (mode)
{
ans.PB(u);
for (y=g[u]->child;y;y=y->next) dfsII(y->pos,0);
}
else
for (y=g[u]->child;y;y=y->next) (y->pos==g[u]->withoutTo)?dfsII(y->pos,1):dfsII(y->pos,0);
}
void theSolver()
{
dfsI(1);dfsII(1,0);
cout << g[1]->without * 1000 << endl;
sort(ans.begin(),ans.end());
for (vInt::iterator e=ans.begin();e!=ans.end();e++) cout << *e << " ";cout << endl;
}
int main()
{
ios::sync_with_stdio(false);
theIniter();
theSolver();
return 0;
}