Cod sursa(job #550606)

Utilizator boti12botiGal Botond boti12boti Data 9 martie 2011 19:43:31
Problema Componente tare conexe Scor 0
Compilator fpc Status done
Runda Arhiva educationala Marime 1.77 kb
type mat=array[1..700,1..700] of integer;
     vak=array[1..5010] of integer;
     vek=array[1..5010] of boolean;
var f:text; i,j,a,b,n,m,d,h,h1,c:integer; v1,v2:vek; x:mat;  q:vak;   t1:boolean;
procedure df1(k:integer);
  var i:integer; t:boolean;
  begin
  i:=0; t:=false;
    while (i<n) and not t do begin
    inc(i);
    if x[k,i]<>0 then t:=true;
    end;
   if t then begin v1[k]:=true;
    for i:=1 to n do begin
      if ((x[k,i]=1)or(x[k,i]=3))and(not v1[i]) then df1(i);
      end;
  end;
  end;

procedure df2(k:integer);
  var i:integer;  t:boolean;
  begin
  i:=0; t:=false;
    while (i<n) and not t do begin
    inc(i);
    if x[k,i]<>0 then t:=true;
    end;
    if t then begin v2[k]:=true;
    for i:=1 to n do begin
      if ((x[k,i]=2)or(x[k,i]=3))and(not v2[i]) then df2(i);
      end;
  end;    end;
begin
  assign(f,'ctc.in');
  reset(f);    d:=0; h:=0;
  readln(f,n,m);
  for i:=1 to m do begin
    readln(f,a,b);
    if x[a,b]=0 then begin x[a,b]:=1; x[b,a]:=2; end
    else begin x[a,b]:=3; x[b,a]:=3; end;
  end;
  close(f);
  a:=0;  b:=0;
  assign(f,'ctc.out');
  rewrite(f);
    for i:=1 to n do begin
    v1[i]:=false;
    v2[i]:=false;
    end;

  repeat
  i:=1;
   while (i<n) and v2[i] do begin inc(i); end;
   a:=i;
  for i:=1 to n do begin
    v1[i]:=false;
    v2[i]:=false;
    end;

  df1(a);

  df2(a);
           inc(d);     h1:=0;

  for i:=1 to n do
   if  v1[i] and  v2[i] then begin inc(b);inc(h1); end;
   inc(h); q[h]:=h1;
  for i:=1 to n do
   if v1[i] and v2[i] then begin inc(h); q[h]:=i; end;
  until b=n;

  write(f,d); c:=0;
 { while c<h do begin
  inc(c);
  writeln(f);
    for j:=1 to q[c] do  begin
      inc(c); write(f,q[c],' ');
      end;
      end;       }
  close(f);
end.