Cod sursa(job #1414433)

Utilizator ButnaruButnaru George Butnaru Data 2 aprilie 2015 16:53:49
Problema Parcurgere DFS - componente conexe Scor 100
Compilator fpc Status done
Runda Arhiva educationala Marime 0.75 kb
program dfs;
type
lista=^date;
date=record
m:longint;
next:lista;
end;
tabel=array[0..100001] of lista;
tabb=array[0..100001] of byte;
var
t:tabel; fr,ff1,ff2:tabb;
a:lista;
n,m,i,j,x,y,sol:longint;
f1,f2:text;
procedure dfs(x:longint);
var a:lista;
begin
a:=t[x]; fr[x]:=1;
while a<>nil do begin
if fr[a^.m]=0 then dfs(a^.m);
a:=a^.next;
end;
end;
begin
assign (f1,'dfs.in');
assign (f2,'dfs.out');
reset (f1);
rewrite (f2);
settextbuf(f1,ff1);
settextbuf(f2,ff2);
readln (f1,n,m);
for i:=1 to m do begin
readln (f1,x,y);
new(a); a^.m:=y; a^.next:=t[x]; t[x]:=a;
new(a); a^.m:=x; a^.next:=t[y]; t[y]:=a;
end;
sol:=0;
for i:=1 to n do
if fr[i]=0 then begin dfs(i); sol:=sol+1; end;
writeln (f2,sol);
close (f1);
close (f2);
end.