Cod sursa(job #2634636)

Utilizator oldatlantianSerban Cercelescu oldatlantian Data 11 iulie 2020 19:33:20
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.48 kb
#include <bits/stdc++.h>
using namespace std;

ifstream fi("dfs.in");
ofstream fo("dfs.out");

const int N = 1e5 + 5;

vector<int> g[N];
bool f[N];

void dfs(int nod) {
	f[nod] = true;
	for (auto v: g[nod]) if (!f[v]) {
		dfs(v);
	}
}

int main() {
	int ans, n, m;

	fi >> n >> m;
	for (int a, b, i = 0; i < m; ++i) {
		fi >> a >> b;
		g[a].push_back(b);
		g[b].push_back(a);
	}

	ans = 0;
	for (int i = 1; i <= n; ++i) if (!f[i]) {
		dfs(i);
		ans+= 1;
	}

	fo << ans << endl;

	return 0;
}