Cod sursa(job #906085)

Utilizator zig_zagFMI Alexandru Gabriel zig_zag Data 6 martie 2013 14:41:28
Problema Parcurgere DFS - componente conexe Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.59 kb
#include <fstream>
#include <vector>
using namespace std;

ifstream in("dfs.in"); ofstream out("dfs.out");
int n,m; bool b[100001];

vector <int> v[100001];

void read()
{
	int x,y;
	in >> n >> m;
	for (int i=1; i<=m; i++)
	{
		in >> x >> y;
		v[x].push_back(y);
		v[y].push_back(x);
	}
}

void dfs(int i)
{
	int j;
	for(j=0; j<v[i].size(); j++)
		if (b[v[i][j]]== false)
		{
			b[v[i][j]]=true;
			dfs(b[v[i][j]]);
		}
}

int main ()
{
	int nr=0;
	read();
	for (int i=1; i<=n; i++)
		if (b[i]==false)
		{
			nr++;
			b[i]=true;
			dfs(i);
		}
	out << nr;
}