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 1069. Prufer Code

..I am O(n^2) too and got AC..
Posted by grayluck 13 Oct 2009 15:09
I use a very simple algorithm..my code is very short..
*just (for i:=1 to n do)check if the number appears in the A set of numbers after i.
*set down the father of each number.
*Just output.

(i'm sorry that my English is really bad..)

var
  con,temp,i,j:longint;
  fin:array[1..7500]of boolean;
  b,a,fa:array[1..7500]of integer;
begin
  {$IFNDEF ONLINE_JUDGE}
  assign(input,'input.in');reset(input);
  assign(output,'output.out');rewritE(output);
  {$ENDIF}
  reaD(temp);
  con:=0;
  while temp<>0 do
    begin
      inc(con);
      a[con]:=temp;
      inc(b[temp]);
      read(temp);
    end;
  for i := 1 to con do
    for j := 1 to con+1 do
      if (not fin[j])and(b[j]=0) then
        begin
          fin[j]:=true;
          fa[j]:=a[i];
          deC(b[a[i]]);
          break;
        end;
  for i := 1 to con+1 do
    begin
      write(i,':');
      for j := 1 to con+1 do
        if (fa[j]=i)or(fa[i]=j) then
          write(' ',j);
      writeln;
    end;
end.

Edited by author 13.10.2009 15:09