Cod sursa(job #830383)

Utilizator antonioteoZait Teodor Antonio antonioteo Data 6 decembrie 2012 19:22:22
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.68 kb
#include <fstream>
#include <vector>
using namespace std;
const char iname[] = "dfs.in";
const char oname[] = "dfs.out";
ifstream fin(iname);
ofstream fout(oname);
int N , M , i , j , x , y , sol;
bool viz[ 100005 ];
vector < int > v[ 100005 ];
void DFS ( int nod )
{
	vector < int > :: iterator it;
	viz[ nod ] = 1;
	for ( it = v[ nod ].begin(); it != v[ nod ].end(); ++it )
		if ( !viz[ *it ] )
			DFS( *it );
}
int main()
{
	fin >> N >> M;
	for ( i = 1; i <= M; ++i )
	{
		fin >> x >> y;
		v[ x ].push_back( y );
		v[ y ].push_back( x );
	}
	for ( i = 1; i <= N; ++i )
	{
		if ( !viz[ i ] )
		{
			++sol;
			DFS( i );
		}
	}
	fout << sol << '\n';
	return 0;
}