Cod sursa(job #248810)

Utilizator 05_YohnE1 La5c01 05_Yohn Data 26 ianuarie 2009 21:06:58
Problema Parcurgere DFS - componente conexe Scor 70
Compilator fpc Status done
Runda Arhiva educationala Marime 1.08 kb
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:longint;
var i:longint;ok:boolean;
begin
ok:=true;
for i:=1 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;
until x=n+1;
assign(f,'dfs.out');rewrite(f);
writeln(f,y-1);close(f);
end.