Cod sursa(job #1420387)

Utilizator ButnaruButnaru George Butnaru Data 18 aprilie 2015 13:26:46
Problema Parcurgere DFS - componente conexe Scor 100
Compilator fpc Status done
Runda Arhiva educationala Marime 0.72 kb
program dfs;
type
lista=^date;
date=record
m:longint;
next:lista;
end;
     vector1=array[0..100001] of lista;
     vector2=array[0..100001] of longint;
var t:vector1; fr:vector2; a:lista;
    n,m,i,nr,x,y: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);
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;
nr:=0;
for i:=1 to n do
if fr[i]=0 then begin dfs(i); nr:=nr+1; end;
writeln (f2,nr);
close (f1);
close (f2);
end.