Pagini recente » Cod sursa (job #1384379) | Cod sursa (job #2476204) | Cod sursa (job #2492744) | Cod sursa (job #1435736) | Cod sursa (job #436157)
Cod sursa(job #436157)
/*
* File: main.cpp
* Author: VirtualDemon
*
* Created on April 8, 2010, 8:50 AM
*/
#include <vector>
#include <cstdlib>
#include <fstream>
/*
*
*/
using namespace std;
vector< bool > uz;
vector< vector< int > > G;
inline void DFS( int x )
{
uz[x]=true;
vector< int >::const_iterator it=G[x].begin(), iend=G[x].end();
for( ; it < iend; ++it )
if( !uz[*it] )
DFS( *it );
}
int main(int argc, char** argv)
{
int N, M, x, y;
ifstream in( "dfs.in" );
in>>N>>M;
G.resize(N);
uz.resize(N);
for( ; M; --M )
{
in>>x>>y;
--x, --y;
G[x].push_back(y);
G[y].push_back(x);
}
for( x=y=0; x < N; ++x )
if( !uz[x] )
{
++y;
DFS(x);
}
ofstream out( "dfs.out" );
out<<y<<'\n';
return EXIT_SUCCESS;
}