Cod sursa(job #699439)

Utilizator arch_enemyAngela Gossow arch_enemy Data 29 februarie 2012 19:21:22
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.72 kb
#include <cstdio>
#include <cstring>
#include <vector>

using namespace std;

#define NMAX 100001
#define pb push_back

int N, M, CC, i, x, y;
vector< int > G[NMAX];
bool Used[NMAX];

inline void Df( int Nod )
{
	Used[Nod] = true;
	for( vector< int >::iterator it = G[Nod].begin(); it != G[Nod].end(); ++it )
		if( !Used[*it] )
			Df( *it );
}

int main()
{
	freopen("dfs.in", "r", stdin);
	freopen("dfs.out", "w", stdout);
	
	scanf("%d%d", &N, &M );
	for( ; M--; )
	{
		scanf("%d%d", &x, &y );
		G[x].pb( y );
		G[y].pb( x );
	}
	
	memset( Used, false, sizeof(Used) );
	for( i = 1, CC = 0; i <= N; ++i )
		if( !Used[i] )
		{
			++CC;
			Df( i );
		}
	printf("%d\n", CC );
	
	return 0;
}