Cod sursa(job #703034)

Utilizator andrei_toaderToader Andrei Sorin andrei_toader Data 2 martie 2012 10:35:48
Problema Componente tare conexe Scor 0
Compilator fpc Status done
Runda Arhiva educationala Marime 1.35 kb
program ctc;
var f,g:text;
    n,m,nr,c,i,j,x,y:longint;
    a,b:array of array of longint;
    viz,viz1:array of 0..1;
    v:array of longint;

procedure dfs (nod:longint);
var j:longint;
begin
 viz[nod]:=1;
 for j:=1 to a[nod,0] do
  if viz[a[nod,j]]=0 then
  begin
   viz[a[nod,j]]:=1;
   dfs(a[nod,j]);
   nr:=nr+1;
   v[nr]:=nod;
  end;
end;

procedure dfst (nod:longint);
var j:longint;
begin
 viz1[nod]:=1;
 setlength (a[c],length (a[c])+1);
 a[c,0]:=a[c,0]+1;
 a[c,a[c,0]]:=nod;
 for j:=1 to b[nod,0] do
  if viz1[b[nod,j]]=0 then
  begin
   dfst (b[nod,j]);
  end;
end;

begin
 assign(f,'ctc.in'); reset (F);
 assign(G,'ctc.out'); rewrite (G);
 readln (f,n,m);
 setlengtH (a,n+1,1);
 setlength (viz,n+1);
 setlength (b,n+1,1);
 setlengtH (v,n+1);
 for i:=1 to m do
 begin
  readln (f,x,y);
  setlengtH (a[x],length (a[x])+1);
  a[x,0]:=a[x,0]+1;
  a[x,a[x,0]]:=y;
  setlength (b[y],length (b[y])+1);
  b[y,0]:=b[y,0]+1;
  b[y,b[y,0]]:=x;
 end;
 nr:=0;
 for i:=1 to n do
  if viz[i]=0 then
   dfs (i);
  setlength (viz1,n+1);
 c:=0;
 for i:=nr downto 1 do
  if viz1[v[i]]=0 then
  begin
  c:=c+1;  a[c,0]:=0;
  setlength (a[c],1);
   dfst(v[i]);
  end;
  writeln (G,c);
  for i:=1 to c do
  begin
   for j:=1 to a[i,0] do
    write (g,a[i,j],' ');
   writeln (g);
  end;
  close (F); close (G);
end.