type link=^pnode; pnode=record w:longint; next:link; end; var a,root:array[0..1122] of longint; g:array[0..1100,0..1100] of longint; hash:array[0..1122] of boolean; list:array[0..1122] of link; n,min:longint; procedure init; begin assign(input,‘badnews.in‘); assign(output,‘badnew.out‘); reset(input); rewrite(output); end; procedure terminate; begin close(input); close(output); halt; end; procedure qsort(s,t:longint); var i,j,x,m:longint; begin m:=(s+t)>>1; x:=a[m]; a[m]:=a[s]; a[s]:=x; i:=s; j:=t; repeat while (i<j) and (x>a[j]) do dec(j); if i<j then begin a[i]:=a[j]; inc(i); end; while (i<j) and (x<a[i]) do inc(i); if i<j then begin a[j]:=a[i]; dec(j); end; until i=j; a[i]:=x; inc(i); dec(j); if i<t then qsort(i,t); if s<j then qsort(s,j); end; function dfs(i:longint):longint; var j,tot:longint; p,q:link; pd:boolean; begin pd:=true; p:=nil; for j:=1 to g[i][0] do begin if not hash[g[i][j]] then begin pd:=false; hash[g[i][j]]:=true; new(q); q^.w:=dfs(g[i][j])+1; q^.next:=p; p:=q; end; end; if pd then exit(1); tot:=0; while p<>nil do begin inc(tot); a[tot]:=p^.w; p:=p^.next; end; qsort(1,tot); for j:=2 to tot do inc(a[j],j-1); dfs:=0; for j:=1 to tot do if a[j]>dfs then dfs:=a[j]; end; procedure main; var i,x,min:longint; begin fillchar(list,sizeof(list),0); readln(n); for i:=2 to n do begin readln(x); inc(g[i][0]); g[i][g[i][0]]:=x; inc(g[x][0]); g[x][g[x][0]]:=i; end; for i:=1 to n do begin fillchar(hash,sizeof(hash),0); hash[i]:=true; root[i]:=dfs(i); end; min:=10000; for i:=1 to n do begin if root[i]<min then min:=root[i]; end; writeln(min); a[0]:=0; for i:=1 to n do begin if root[i]=min then begin inc(a[0]); a[a[0]]:=i; end; end; for i:=1 to a[0]-1 do write(a[i],‘ ‘); writeln(a[a[0]]); end; begin init; main; terminate; end.
喜欢就收藏一下,vic私人qq:1064864324,加我一起讨论问题,一起进步^-^
时间: 2024-10-12 22:47:25