Cod sursa(job #248814)

Utilizator 05_YohnE1 La5c01 05_Yohn Data 26 ianuarie 2009 21:11:22
Problema Parcurgere DFS - componente conexe Scor 100
Compilator fpc Status done
Runda Arhiva educationala Marime 1.17 kb
{$A+,B-,D+,E+,F-,G-,I+,L+,N-,O-,P-,Q-,R-,S-,T-,V+,X+}
{$M 16384,0,655360}
type pvect=^vect;
     vect=record
          info:longint;
          urm:pvect;
          end;
var vec:array[1..100000]of pvect;
    viz:array[1..100000]of boolean;
    n,m,x,y,i:longint;
    f:text;
procedure init(var p:pvect;a:longint);
var q:pvect;
begin
new(q);q^.info:=a;
if p=nil then begin
              q^.urm:=nil;
              p:=q;
     end else begin
              q^.urm:=p;
              p:=q;
              end;
end;

function caut(x:longint):longint;
var i:longint;ok:boolean;
begin
ok:=true;
for i:=x to n do if not viz[i] then begin caut:=i;ok:=false;break;end;
if ok then caut:=n+1;
end;

procedure parcurg(x:longint);
var p:pvect;
begin
viz[x]:=true;
p:=vec[x];
while p<>nil do begin
      if not viz[p^.info] then parcurg(p^.info);
      p:=p^.urm;
      end;
end;

begin
assign(f,'dfs.in');reset(f);
readln(f,n,m);
for i:=1 to m do begin
    readln(f,x,y);
    init(vec[x],y);
    init(vec[y],x);
end;close(f);
x:=1;y:=1;
repeat
parcurg(x);
inc(y);
x:=caut(x);
until x=n+1;
assign(f,'dfs.out');rewrite(f);
writeln(f,y-1);close(f);
end.