Cod sursa(job #347106)

Utilizator florin_marius90Florin Marius Popescu florin_marius90 Data 10 septembrie 2009 23:19:15
Problema Sortare topologica Scor 0
Compilator fpc Status done
Runda Arhiva educationala Marime 0.79 kb
var grad:array[1..50000] of integer;
    tati:array[1..50000] of integer;
    coada:array[1..50000] of integer;
    y,i,n,m,a,b,contor,nod:longint;
    ok:boolean;
    f,g:text;
begin
assign(f,'sortaret.in'); reset(f);
assign(g,'sortaret.out'); rewrite(g);
readln(f,n,m);
for i:=1 to m do
 begin
 readln(f,a,b);
 if tati[b]=0 then grad[a]:=grad[a]+1;
 tati[b]:=a;

 end;
 close(f);   contor:=0;
 for i:=1 to n do
  if grad[i]=0 then
  begin
  {x:=grad[i];}  nod:=i;
  {contor:=contor+1;
  coada[contor]:=i; }
  ok:=true;
  while ok do
   begin
   contor:=contor+1;
   coada[contor]:=nod;
   y:=tati[nod];
   grad[y]:=grad[y]-1;

   if grad[y]<>0 then ok:=false else nod:=y;
  end;
  end;
 for i:=contor downto 1 do write(g,coada[i],' ');
 close(g);
 end.