Pagini recente » Cod sursa (job #3239322) | Cod sursa (job #2391194) | Cod sursa (job #3267194) | Cod sursa (job #2249415) | Cod sursa (job #3262262)
#include <bits/stdc++.h>
using namespace std;
int main() {
ifstream in("dfs.in");
ofstream out("dfs.out");
int n, m;
in >> n >> m;
vector<vector<int>> g(n + 1);
for(int i =1 ; i <= m; i++) {
int x, y;
in >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
vector<bool> seen(n + 1);
function<void(int)> dfs = [&](int node) {
seen[node] = true;
for(auto son : g[node]) {
if(seen[son]) { continue; }
dfs(son);
}
};
int connected_components = 0;
for(int i = 1; i <= n; i++) {
if(seen[i]) { continue; }
dfs(i);
connected_components++;
}
out << connected_components << '\n';
return 0;
}