Pagini recente » Cod sursa (job #178608) | Cod sursa (job #73999) | Cod sursa (job #976313) | Cod sursa (job #666247) | Cod sursa (job #594222)
Cod sursa(job #594222)
const f = 'dfs.in'; g = 'dfs.out';
type pnod = ^nod;
nod = record
next : pnod;
info : longint;
end;
var
a : array[0..100001] of pnod;
viz : array[0..100001] of longint;
x, y, start, m, n : longint;
nrc, i : longint;
p : pnod;
buf, buf1 : array[1..1 shl 17] of char;
procedure add( var dest : pnod; val : longint );
var
q : pnod;
begin
new( q );
q^.info := val;
q^.next := dest;
dest := q;
end;
procedure dfs( start : longint );
var
q : pnod;
begin
viz[start] := 1;
q := a[start];
while q <> nil do begin
if viz[q^.info] = 0 then dfs( q^.info );
q := q^.next;
end;
end;
begin
assign( input,f ); reset( input );
assign( output,g ); rewrite( output );
settextbuf( input,buf );
settextbuf( output,buf1 );
readln(n, m );
for i:=1 to m do begin
readln(x, y);
add( a[x], y );
add( a[y], x );
end;
for i := 1 to n do
if viz[i] = 0 then begin
inc( nrc );
dfs( i );
end;
writeln( nrc );
end.