Cod sursa(job #1109397)

Utilizator cristi103tiron cristian cristi103 Data 17 februarie 2014 09:13:32
Problema Parcurgere DFS - componente conexe Scor 5
Compilator cpp Status done
Runda Arhiva educationala Marime 0.54 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 (a[k][i]==1 && viz [i]==0)
		df (i);
}
int main ()
{
	fin>>n>>m;
	for (int i=1;i<=m;i++)
	{
		fin>>x>>y;
		a[x][y]=1;
		a[y][x]=1;
	}
	for (int j=1;j<=n;j++)
	if (viz[j]==0)
{	df (j);
	++sol;}
	fout<<sol<<" ";
	fin.close();
    fout.close();
	return 0;
}