Cod sursa(job #1405552)

Utilizator ButnaruButnaru George Butnaru Data 29 martie 2015 13:10:16
Problema Parcurgere DFS - componente conexe Scor 100
Compilator fpc Status done
Runda Arhiva educationala Marime 0.7 kb
program dfss;
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:tabb;
a:lista;
n,m,i,j,sol,x,y:longint;
f1,f2:text;
procedure dfs(x:longint);
var a:lista;
begin
fr[x]:=1; a:=t[x];
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);
readln (f1,n,m);
for i:=1 to m do begin
readln (f1,x,y);
new(a); a^.m:=x; a^.next:=t[y]; t[y]:=a;
new(a); a^.m:=y; a^.next:=t[x]; t[x]:=a;
end;
sol:=0;
for i:=1 to n do
if fr[i]=0 then begin sol:=sol+1; dfs(i); end;
writeln (f2,sol);
close (f1);
close (f2);
end.