Cod sursa(job #164429)

Utilizator adalLica Adela adal Data 24 martie 2008 10:43:48
Problema Sortare topologica Scor 0
Compilator fpc Status done
Runda Arhiva educationala Marime 0.88 kb
program sort_top;
type point=^lista;
     lista=record
         inf:longint;
         leg:point;
     end;

var l:array[0..50005] of point;
    c:array[0..50005] of longint;
    sel:array[0..50005] of boolean;
    i,n,x,y,m:longint;
    p:point;
    f,g:text;


procedure df(nod:longint);
var p:point;
begin
    sel[nod]:=true;
    p:=l[nod];
    while p<>nil do begin
        if sel[p^.inf]=false then df(p^.inf);
        p:=p^.leg;
    end;
    inc(c[0]); c[c[0]]:=nod;
end;

begin
    assign(f,'sortaret.in'); reset(f);
    assign(g,'sortaret.out'); rewrite(g);
    readln(f,n,m);
    for i:=1 to n do l[i]:=nil;
    for i:=1 to m do begin
        readln(f,x,y);
        new(p); p^.inf:=y; p^.leg:=l[x]; l[x]:=p;
    end;
    fillchar(sel, sizeof(sel),0);
    df(1);
    for i:=n downto 2 do write(g,c[i],' ');
    writeln(g,c[1]);
    close(g); close(f);
end.