Cod sursa(job #1109403)

Utilizator cristi103tiron cristian cristi103 Data 17 februarie 2014 09:22:39
Problema Parcurgere DFS - componente conexe Scor 5
Compilator cpp Status done
Runda Arhiva educationala Marime 0.57 kb
#include <fstream>
#include <vector>
using namespace std;
ifstream fin ("dfs.in");
ofstream fout ("dfs.out");
const int nmax = 100002;
vector <int> a[nmax];
bool viz[nmax];
int n,m,x,y,sol;
void df (int k)
{
	viz[k]=1;
	for (int i=1;i<=(int)a[k].size();++i)
{
	if (viz[ a[k][i] ] == 0)
		df (i);
	}
}
int main ()
{
	fin>>n>>m;
	for (int i=1;i<=m;i++)
	{
		fin>>x>>y;
		a[x].push_back( y );
        a[y].push_back( x );
	}
	for (int j=1;j<=n;j++)	{
	
	if (viz[j]==0)
{	df (j);
	++sol;} }
	fout<<sol<<" ";
	fin.close();
    fout.close();
	return 0;
}