1 /**************************************************************
2 Problem: 3164
3 User: Tunix
4 Language: C++
5 Result: Accepted
6 Time:648 ms
7 Memory:30304 kb
8 ****************************************************************/
9
10 //Huce #6 B
11 #include<vector>
12 #include<cstdio>
13 #include<cstdlib>
14 #include<cstring>
15 #include<iostream>
16 #include<algorithm>
17 #define rep(i,n) for(int i=0;i<n;++i)
18 #define F(i,j,n) for(int i=j;i<=n;++i)
19 #define D(i,j,n) for(int i=j;i>=n;--i)
20 using namespace std;
21
22 int getint(){
23 int v=0,sign=1; char ch=getchar();
24 while(ch<‘0‘||ch>‘9‘) {if (ch==‘-‘) sign=-1; ch=getchar();}
25 while(ch>=‘0‘&&ch<=‘9‘) {v=v*10+ch-‘0‘; ch=getchar();}
26 return v*sign;
27 }
28 typedef long long LL;
29 const int N=400010,INF=~0u>>2;
30 /*******************tamplate********************/
31 int hd[N],to[N],ne[N],cnt;
32 void add(int x,int y){
33 to[++cnt]=y; ne[cnt]=hd[x]; hd[x]=cnt;
34 }
35 struct node{int v,l,r,dis;}t[N];
36 #define L t[x].l
37 #define R t[x].r
38 int n,m,black[N],white[N],rt[N],mn[N],tot,fa[N];
39
40 int merge(int x,int y){
41 if (!x || !y) return x+y;
42 if (t[x].v>t[y].v) swap(x,y);
43 R=merge(R,y);
44 if (t[L].dis<t[R].dis) swap(L,R);
45 t[x].dis=t[R].dis+1;
46 return x;
47 }
48 bool sign[N];
49 void dfs(int x,bool now){
50 if (!hd[x]){
51 mn[x]=1;
52 rt[x]=++tot;
53 t[tot].v=x;
54 t[tot].l=t[tot].r=t[tot].dis=0;
55 return;
56 }
57 if (sign[x]==now) mn[x]=INF; else mn[x]=0;
58 for(int i=hd[x];i;i=ne[i]){
59 sign[to[i]]=sign[x]^1;
60 dfs(to[i],now);
61 if (sign[x]==now){
62 if (mn[x]>mn[to[i]]){
63 mn[x]=mn[to[i]];
64 rt[x]=rt[to[i]];
65 }else if (mn[x]==mn[to[i]]){
66 rt[x]=merge(rt[x],rt[to[i]]);
67 }
68 }else{
69 mn[x]+=mn[to[i]];
70 rt[x]=merge(rt[x],rt[to[i]]);
71 }
72 }
73 }
74 int ans[N];
75 int main(){
76 #ifndef ONLINE_JUDGE
77 freopen("B.in","r",stdin);
78 // freopen("output.txt","w",stdout);
79 #endif
80 n=getint();
81 F(i,2,n){
82 fa[i]=getint(); add(fa[i],i);
83 }
84 tot=0; sign[1]=1;
85 dfs(1,1);
86 int num1=0;
87 while(rt[1]){black[++num1]=t[rt[1]].v; rt[1]=merge(t[rt[1]].l,t[rt[1]].r);}
88 sort(black+1,black+num1+1);
89 tot=0; memset(rt,0,sizeof rt);
90 dfs(1,0);
91 int num2=0;
92 while(rt[1]){white[++num2]=t[rt[1]].v; rt[1]=merge(t[rt[1]].l,t[rt[1]].r);}
93 sort(white+1,white+num2+1);
94 int num=0;
95 for(int i=1,j=1;i<=num1 && j<=num2;i++){
96 while(black[i]>white[j] && j<=num2) j++;
97 if (black[i]==white[j]) ans[++num]=black[i];
98 }
99 int sum=0;
100 F(i,1,num) sum^=ans[i];
101 printf("%d %d %d\n",ans[1],num,sum);
102 return 0;
103 }
104