Cod sursa(job #672921)

Utilizator BitOneSAlexandru BitOne Data 3 februarie 2012 14:12:57
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.61 kb
#include <vector>
#include <fstream>
#include <cstdlib>
#define N_MAX 100011

using namespace std;

bool was[N_MAX];
vector< int > G[N_MAX];

inline void DFS( int x )
{
	was[x]=true;
	vector< int >::const_iterator it=G[x].begin(), iend=G[x].end();
	
	for( ; it < iend; ++it )
		if( false == was[*it] )
			DFS(*it);
}
int main()
{
	int N, M, x, y, i, count=0;
	ifstream in( "dfs.in" );
	ofstream out( "dfs.out" );
	
	for( in>>N>>M; M; --M )
	{
		in>>x>>y;
		G[x].push_back(y);
		G[y].push_back(x);
	}
	for( i=1; i <= N; ++i )
		if( false == was[i] )
		{
			DFS(i);
			++count;
		}
	out<<count<<'\n';
	
	return EXIT_SUCCESS;
}