Cod sursa(job #2611507)

Utilizator Arteni_CristiArteni Cristi Arteni_Cristi Data 6 mai 2020 23:24:28
Problema Componente tare conexe Scor 0
Compilator fpc Status done
Runda Arhiva educationala Marime 1.11 kb
var t:array[1..100005,1..100005] of longint;
    viz,cd,q,p:array[1..100005] of longint;
    v:array[1..100005] of ansistring;
    n,m,i,j,a,b,x:longint;
    s,z:ansistring;
begin
assign(input,'ctc.in'); reset(input);
assign(output,'ctc.out'); rewrite(output);
readln(n,m);
for i:=1 to m do
 begin
  readln(a,b);
  t[a,b]:=1
 end;
for i:=1 to n do if viz[i]=0 then
 begin
  for j:=1 to n do
   begin
    q[j]:=0;
    p[j]:=0
   end;
  a:=1; b:=1; cd[a]:=i; q[i]:=1;
  while a<=b do
   begin
    for j:=1 to n do
     if (q[j]=0) and (t[cd[a],j]=1) then
      begin
       inc(b);
       cd[b]:=j;
       q[j]:=1
      end;
    inc(a)
   end;
  a:=1; b:=1; cd[a]:=i; p[i]:=1;
  while a<=b do
   begin
    for j:=1 to n do
     if (p[j]=0) and (t[j,cd[a]]=1) then
      begin
       inc(b);
       cd[b]:=j;
       p[j]:=1
      end;
    inc(a)
   end;
  inc(x); s:='';
  for j:=1 to n do
   if (q[j]=1) and (p[j]=1) then
    begin
     viz[j]:=x;
     str(j,z);
     s:=s+z+' '
    end;
  v[x]:=s
 end;
writeln(x);
for i:=1 to x do writeln(v[i]);
close(input);
close(output)
end.