Cod sursa(job #410981)

Utilizator saodem74hieu tran saodem74 Data 4 martie 2010 17:47:01
Problema Componente tare conexe Scor 0
Compilator fpc Status done
Runda Arhiva educationala Marime 1.93 kb
uses    math;
const   tfi='ctc.in';
        tfo='ctc.out';
        maxn=100100;
        maxm=200200;
type    li=record
                    u,v:longint;
                end;
var     fi,fo:text;
        cc,sl,cnt,top,n,m:longint;
        ds:array[0..maxm] of li;
        st,ke:array[0..maxm] of longint;
        c,b,stack,num,low:array[0..maxn] of longint;

procedure enter;
var     i,j:longint;
begin
        read(fi,n,m);
        for i:=1 to m do
         with ds[i] do
          begin
           read(fi,u,v);
           inc(st[u]);
          end;
          inc(st[1]);
          for i:=2 to n+1 do st[i]:=st[i]+st[i-1];
         for i:=1 to m do
          with ds[i] do
           begin
               dec(st[u]);
               ke[st[u]]:=v;
           end;
end;

procedure dfs(u:longint);
var     i,v:longint;
begin
        inc(cnt);
        num[u]:=cnt; low[u]:=cnt;
        inc(top);
        stack[top]:=u;
        for i:=st[u] to st[u+1]-1 do
         begin
          v:=ke[i];
          if num[v]<>0 then low[u]:=min(low[u],num[v])
           else
            begin
              dfs(v);
              low[u]:=min(low[u],low[v]);
            end;
         end;
        if low[u]=num[u] then
         begin
          inc(cc);
          repeat
                v:=stack[top];
                dec(top);
                inc(sl);
                c[sl]:=v;
          until v=u;
          b[cc]:=sl;
        end;
end;

procedure process;
var     j,i:longint;
begin
        cnt:=0;
        for i:=1 to n do
         if num[i]=0 then
          begin
           dfs(i);
          end;
        writeln(fo,cc);
        for i:=1 to cc do
         begin
          for j:=b[i-1]+1 to b[i] do write(fo,c[j],' ');
          writeln(fo);
         end;
end;

begin
        assign(Fi,tfi); reset(fi);
        assign(fo,tfo); rewrite(fo);
        enter;
        process;
        close(fi); close(Fo);
end.