Cod sursa(job #2399101)

Utilizator robertstrecheStreche Robert robertstreche Data 6 aprilie 2019 21:25:28
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.57 kb
#include <fstream>
#include <vector>

#define NMAX 100005

using namespace std;

vector <int> edges[NMAX];
bool ap[NMAX];

void dfs(int node) {
	for (auto it : edges[node]) {
		if (!ap[it]) {
			ap[it] = 1;
			dfs(it);
		}
	}
}

int main()
{
	ifstream f("dfs.in");
	ofstream g("dfs.out");

	int n, m, x, y, comp = 0;

	f >> n >> m;
	for (int i = 0; i < m; i++) {
		f >> x >> y;
		edges[x].push_back(y);
		edges[y].push_back(x);
	}

	for (int i = 1; i <= n; i++) {
		if (!ap[i]) {
			ap[i] = 1;
			comp++;
			dfs(i);
		}
	}

	g << comp << '\n';
}