ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1056. Centers of the Net

Why I got WA!!
Posted by qwt 10 Feb 2002 13:38
var
  fa,a,x:array[1..10000] of integer;

  mm,m,j,n,i,k,q,r:integer;

begin
  readln(n);
  fillchar(fa,sizeof(fa),0);x:=fa;
  m:=maxint;
  for i:=2 to n do read(fa[i]);
  for i:=1 to n do begin
    fillchar(a,sizeof(A),0);
    j:=i;k:=1;
    while j<>1 do begin
      a[j]:=k;
      inc(k);
      j:=fa[j];
    end;
    a[1]:=k;
    if mm<k then mm:=k;
    for r:=i+1 to n do if a[r]=0 then begin
      j:=r;
      k:=0;
      while a[j]=0 do begin
        inc(k);
        j:=fa[j];
      end;q:=j;
      j:=r;
      if mm<k+a[q] then mm:=k+a[q];
      while a[j]=0 do begin
        a[j]:=a[q]+k;
        dec(k);
        j:=fa[j];
      end;
    end;
    x[i]:=mm-1;
    if x[i]<m then m:=x[i];
  end;
  for i:=1 to n do if m=x[i] then write(i,' ');writeln;
end.