Cod sursa(job #2188957)

Utilizator mihai50000Mihai-Cristian Popescu mihai50000 Data 27 martie 2018 16:24:26
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.5 kb
#include <fstream>
#include <vector>
using namespace std;
ifstream f("dfs.in");
ofstream g("dfs.out");
vector<int> v[100005];
bool viz[100005];
int n, m, x, y, nr_c;
void dfs(int nod)
{
	viz[nod] = 1;
	for (int i = 0; i<v[nod].size(); ++i)
		if (!viz[v[nod][i]])
			dfs(v[nod][i]);
}
int main()
{
	f >> n >> m;
	while(m--)
	{
		f >> x >> y;
		v[x].push_back(y);
		v[y].push_back(x);
	}
	for (int i = 1; i <= n; i++)
		if (!viz[i])
		{
			dfs(i);
			nr_c++;
		}
	g << nr_c;
	return 0;
}