Cod sursa(job #459954)
#include <vector>
#include <cstdlib>
#include <fstream>
#define Nmax 100111
/*
*
*/
using namespace std;
bool was[Nmax];
vector< int > G[Nmax];
inline void DFS( int x )
{
was[x]=true;
if( G[x].empty() )
return;
vector< int >::const_iterator it=G[x].begin(), iend=G[x].end();
for( ; it < iend; ++it )
if( false == was[*it] )
DFS(*it);
}
int main( void )
{
int N, M, x, y, nr=0;
ifstream in( "dfs.in" );
for( in>>N>>M; M; --M )
{
in>>x>>y;
G[x].push_back( y );
G[y].push_back( x );
}
for( x=1; x <= N; ++x )
if( false == was[x] )
{
++nr;
DFS( x );
}
ofstream out( "dfs.out" );
out<<nr<<'\n';
return EXIT_SUCCESS;
}