Cod sursa(job #153270)

Utilizator petrePajarcu Alexandru-Petrisor petre Data 10 martie 2008 12:50:55
Problema Sortare topologica Scor 100
Compilator fpc Status done
Runda Arhiva educationala Marime 1 kb
var A,dir,b:array[1..100000] of longint;
x,gr,y:array[1..50001] of longint;
m,n,i,j,k,l:longint;
begin
assign(input,'sortaret.in');
assign(output,'sortaret.out');
reset(input);
rewrite(output);
readln(n,M);
for i:=1 to m do
        begin
        read(a[i],b[i]);
        inc(y[a[i]]);
        inc(gr[b[i]]);
        end;
for i:=2 to n do
        y[i]:=y[i]+y[i-1];
        y[n+1]:=m;
for i:=1 to m do
        begin
        dir[y[a[i]]]:=b[i];
        dec(y[a[i]]);
        end;
k:=0;
for i:=1 to n do
     if gr[i]=0 then
                 begin
                 inc(K);
                 x[k]:=i;
                 end;
i:=0;
while k<n do
begin
inc(i);
for j:=y[x[i]]+1 to y[x[i]+1] do
        begin
        dec(gr[dir[j]]);
        if gr[dir[j]]=0 then
                        begin
                        inc(K);
                        x[k]:=dir[j];
                        end;
        end;
end;
for i:=1 to n do write(x[i],' ');
close(input);
close(output);
end.