Pagini recente » Cod sursa (job #1777108) | Cod sursa (job #2715027) | Cod sursa (job #1367401) | Cod sursa (job #1719380) | Cod sursa (job #846636)
Cod sursa(job #846636)
program dfs;
type vdynamic=array of longint;
var f1,f2:text;
n,m,i,x,y,k:longint;
a:array [1..100000] of vdynamic;
viz:array [1..100000] of byte;
t:boolean;
procedure dfs(x:longint);
var j:longint;
begin
viz[x]:=1;
for j:=1 to length(a[x])-1 do
if viz[a[x,j]]=0 then dfs(a[x,j]);
end;
begin
assign(f1,'dfs.in');
reset(f1);
assign(f2,'dfs.out');
rewrite(f2);
readln(f1,n,m);
for i:= 1 to n do setlength(a[i],1);
for i:=1 to m do
begin
readln(f1,x,y);
setlength(a[x],length(a[x])+1);
a[x,length(a[x])-1]:=y;
setlength(a[y],length(a[y])+1);
a[y,length(a[y])-1]:=x;
end;
t:=true;
while t do
begin
t:=false;
i:=1;
while (viz[i]<>0)and(i<=n) do inc(i);
if i<>n+1 then begin inc(k);dfs(i);t:=true;end;
end;
writeln(f2,k);
close(f1);close(f2);
end.