Cod sursa(job #1289854)

Utilizator vladrochianVlad Rochian vladrochian Data 10 decembrie 2014 13:49:23
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.53 kb
#include <fstream>
#include <vector>
using namespace std;

const int kMaxN = 100005;

ifstream fin("dfs.in");
ofstream fout("dfs.out");

int N, M, sol;
vector<int> G[kMaxN];
bool viz[kMaxN];

void DFS(int node) {
	viz[node] = true;
	for (int i : G[node])
		if (!viz[i])
			DFS(i);
}

int main() {
	fin >> N >> M;
	while (M--) {
		int x, y;
		fin >> x >> y;
		G[x].push_back(y);
		G[y].push_back(x);
	}
	for (int i = 1; i <= N; ++i)
		if (!viz[i]) {
			++sol;
			DFS(i);
		}
	fout << sol << "\n";
	return 0;
}