Pagini recente » Cod sursa (job #1210772) | Cod sursa (job #1816636) | Cod sursa (job #961710) | Cod sursa (job #986053) | Cod sursa (job #552738)
Cod sursa(job #552738)
program tare_conexitate;
type nod=^graf;
graf=record
inf:longint;
urm:nod;
end;
var a,v:array[1..100000]of nod;f,g:text;
viz:array[1..200000]of 0..1;p,q:nod;
c,post,rez:array[1..300000]of longint;
n,m,i,x,y,nr,nrc,st:longint;
procedure dfs(x:longint);
var p:nod;
begin
viz[x]:=1;
p:=v[x];
while p<>nil do begin
if viz[p^.inf]=0 then dfs(p^.inf);
p:=p^.urm;
end;
inc(nr);
post[nr]:=x;
end;
procedure dfst(x:longint);
var q:nod;
begin
viz[x]:=0; inc(st);rez[st]:=x;
q:=a[x];
while q<>nil do begin
if viz[q^.inf]<>0 then dfst(q^.inf);
q:=q^.urm;
end;
end;
begin
assign(f,'ctc.in');reset(f);
assign(g,'ctc.out');rewrite(g);
readln(f,n,m);
for i:=1 to m do begin
readln(f,x,y);
new(p); p^.inf:=y; p^.urm:=v[x]; v[x]:=p;
new(q); q^.inf:=x; q^.urm:=a[y]; a[y]:=q;
end;
for i:=1 to n do
if viz[i]=0 then dfs(i);
for i:=n downto 1 do
if viz[post[i]]<>0 then begin
inc(nrc);
dfst(post[i]);
inc(st);
rez[st]:=-1;
end;
writeln(g,nrc);
for i:=1 to st do
if rez[i]>0 then write(g,rez[i],' ')
else writeln(g);
dispose(q);dispose(p);
close(f);close(g);
end.