Cod sursa(job #1086932)

Utilizator alexcruceruAlexandru Cruceru alexcruceru Data 18 ianuarie 2014 18:26:24
Problema Parcurgere DFS - componente conexe Scor 5
Compilator fpc Status done
Runda Arhiva educationala Marime 0.54 kb
program nrdecomponenteconexe;
type sir=array[1..1000]of word;
var f,g:text;
    co:sir;
    n,m,i,j,k,x,y:word;


begin
assign(f,'dfs.in');reset(f);
assign(g,'dfs.out');rewrite(g);
readln(f,n,m);
k:=0;
for i:=1 to n do co[i]:=0;
for i:=1 to m do
  begin
  readln(f,x,y);
  if (co[x]=0)and(co[y]=0) then begin inc(k); co[x]:=k; co[y]:=k; end
    else if co[x]=0 then co[x]:=co[y]
      else if co[y]=0 then co[y]:=co[x];
  end;
for i:=1 to n do
  if co[i]=0 then inc(k);

write(g,k);
close(f);
close(g);
end.