Cod sursa(job #348400)

Utilizator florin_marius90Florin Marius Popescu florin_marius90 Data 15 septembrie 2009 18:40:40
Problema Componente tare conexe Scor 0
Compilator fpc Status done
Runda Arhiva educationala Marime 1.66 kb
type nod=^mode;
     mode=record
          x:longint;
          next:nod;
          end;
var G,Gt:array[1..100000] of nod;
    stg,stgt:array[1..100000,1..2] of longint;
    vizg,vizgt:array[1..100000] of 0..1;
    q,a,b,ii,tt,poz,i,n,m,t:longint;
    k,p:nod;
    f,gg:text;
procedure parcurgere;
 begin
 while i<=t do
 begin

 k:=g[stg[i,1]];
 while k<> nil do
  if vizg[k^.x]=0 then begin vizg[k^.x]:=1; t:=t+1; stg[t,1]:=k^.x; k:=k^.next; end else
                                                   k:=k^.next;
 i:=i+1;
 end;
 {i:=i-1; }
 end;

procedure parcurgere2;
begin
while ii<=tt do
 begin
 k:=gt[stgt[ii,1]];
 while k<> nil do
  if (vizg[k^.x]=1) and (vizgt[k^.x]=0)then begin vizgt[k^.x]:=1; tt:=tt+1; stgt[tt,1]:=k^.x; k:=k^.next; end else
                                                   k:=k^.next;
 ii:=ii+1;
 end;
{ ii:=ii-1;  }

end;



begin
assign(f,'ctc.in'); reset(f);
assign(gg,'ctc.out') ; rewrite(gg);
readln(f,n,m);
for i:=1 to m do
 begin
 readln(f,a,b);
 new(p);
 p^.x:=b;
 p^.next:=G[a];
 G[a]:=p;
 new(p);
 p^.x:=a;
 p^.next:=Gt[b];
 Gt[b]:=p;

 end;
 {stg[1]:=1; stgt[1]:=1;}  poz:=0; t:=0; i:=1;   tt:=0;  ii:=1;
 while t<n  do
  begin
  q:=1;
  while q<=n do
   if vizg[q]=0 then begin t:=t+1; stg[t,1]:=q; ; vizg[q]:=1;{stgt[i,1]:=q;} q:=n+1; end else q:=q+1;
  parcurgere;
  while tt<t  do
  begin
  stgt[ii,1]:=stg[ii,1]; tt:=tt+1;  vizgt[stg[ii,1]]:=1;
  parcurgere2;
  stgt[ii-1,2]:=2;   inc(poz);
  end;

  end;

  writeln(gg,poz);
  for i:=1 to n do
   begin
   write(gg,stgt[i,1],' ');
   if stgt[i,2]=2 then writeln(gg);
   end;
   close(f); close(gg);
end.