Pagini recente » Cod sursa (job #230764) | Cod sursa (job #238966) | Profil A_Person_135 | Cod sursa (job #355619) | Cod sursa (job #172057)
Cod sursa(job #172057)
program sortare_topologica;
type point=^nod;
nod=record
crt:longint;
uf:point;
end;
var n,m,i,x,y:longint;
fii:array[1..50000] of point;
viz:array[1..50000] of byte;
f1,f2:text;
l:point;
procedure introduce_date(tata,fiu:longint);
var p:point;
begin
new(p);
p^.crt:=fiu;
p^.uf:=fii[tata];
fii[tata]:=p;
end;
procedure dfs(d:longint);
var p:point;
begin
viz[d]:=1;
p:=fii[d];
while p<>nil do
begin
if viz[p^.crt]=0 then dfs(p^.crt);
p:=p^.uf;
end;
new(p);
p^.crt:=d;
p^.uf:=l;
l:=p;
end;
begin
assign(f1,'sortaret.in');reset(f1);
assign(f2,'sortaret.out');rewrite(f2);
readln(f1,n,m);
for i:=1 to m do
begin
readln(f1,x,y);
introduce_date(x,y);
end;
for i:=1 to n do
if viz[i]=0 then dfs(i);
while l<>nil do
begin
write(f2,l^.crt,' ');
l:=l^.uf;
end;
close(f2);
end.