what is test #1?
All the test I made up was ok.
but I can't pass test #1!
please halp me!
program gh;
type
node=record
v:boolean;
w,e1,r:longint;
x,e:array[1..100] of integer;
end;
var
a:array[1..10000] of node;
d:array[1..200] of integer;
s,f,g,h,j,k,l,m,n,i,d1:longint;
procedure work(q:integer);
var
z,c,b,i,j:longint;
begin
a[q].w:=0;
a[q].e1:=0;
for i:=1 to a[q].r do
begin
if a[a[q].x[i]].v=true then
begin
if a[a[q].x[i]].w+1>a[q].w then
begin
a[q].w:=a[a[q].x[i]].w+1;
a[q].e1:=a[q].e1+1;
a[q].e[a[q].e1]:=a[q].x[i];
end
end
else
begin
work(a[q].x[i]);
if a[a[q].x[i]].w+1>a[q].w then
begin
a[q].w:=a[a[q].x[i]].w+1;
a[q].e1:=1;
a[q].e[a[q].e1]:=a[q].x[i];
end
else
begin
if a[a[q].x[i]].w+1=a[q].w then
begin
a[q].e1:=a[q].e1+1;
a[q].e[a[q].e1]:=a[q].x[i];
end;
end;
end;
end;
a[q].v:=true;
end;
procedure work1(q,z:integer);
var
c,j,c1,b1,i,b,k,l:longint;
begin
if (a[q].e1>=2) or (a[q].w=z+1) then
begin
writeln(q);
halt;
end
else
begin
if a[q].w>z+1 then
begin
if a[q].w=z+2 then
begin
writeln(q,' ',a[q].e[1]);
halt;
end
else
begin
c:=0;
for i:=1 to a[q].r do
if (a[a[q].x[i]].w+1<a[q].w) and (a[a[q].x[i]].w+1>c) then
c:=a[a[q].x[i]].w;
if c>z then
z:=c;
if a[q].w=z+2 then
writeln(q,' ',a[q].e[1])
else
work1(a[q].e[1],z+1);
end;
end;
end;
end;
begin
read(n);
for i:=2 to n do
begin
read(s);
a[s].r:=a[s].r+1;
a[s].x[a[s].r]:=i;
end;
work(1);
if a[1].e1>=2 then
writeln(1)
else
begin
f:=0;
for i:=1 to a[1].r do
if (a[a[1].x[i]].w+1>f) and (a[a[1].x[i]].w+1<a[1].w) then
f:=a[a[1].x[i]].w;
if f=a[1].w-2 then
writeln(1,' ',a[1].e[1])
else
begin
work1(a[1].e[1],f+1);
end;
end;
end.