Nu aveti permisiuni pentru a descarca fisierul grader_test8.in
Cod sursa(job #157730)
Utilizator | Data | 13 martie 2008 11:14:06 | |
---|---|---|---|
Problema | Parcurgere DFS - componente conexe | Scor | 55 |
Compilator | fpc | Status | done |
Runda | Arhiva educationala | Marime | 1 kb |
type lista=^element;
element=record
edge:word;
next:lista;
end;
const inf=maxint;
nmax=10001;
var list:array[1..nmax] of lista;
v:array[1..nmax] of longint;
t,i,n,m,k:longint;
p:lista;
procedure citire;
var a,b:longint;
begin
assign(input,'dfs.in');reset(input);
readln(n,m);
for i:=1 to m do
begin
read(a,b);
new(p);
p^.edge:=a;
p^.next:=list[b];
list[b]:=p;
new(p);
p^.edge:=b;
p^.next:=list[a];
list[a]:=p;
end;
end;
procedure dfs(nod:integer);
var p:lista;
begin
v[nod]:=1;
p:=list[nod];
while p<>nil do
begin
if v[p^.edge]=0 then dfs(p^.edge);
p:=p^.next;
end;
end;
begin {main}
citire;
k:=0;
for t:=1 to n do
if v[t]=0 then
begin
inc(k);
dfs(t);
end;
assign(output,'dfs.out');rewrite(output);
write(k);
close(output);
end.