Pagini recente » Cod sursa (job #2468060) | Cod sursa (job #663474) | Cod sursa (job #380109) | Cod sursa (job #606877) | Cod sursa (job #2388273)
#include <bits/stdc++.h>
using namespace std;
#define cin in
#define cout out
vector<bool> vis;
vector<vector<int>> graph;
ifstream in("dfs.in");
ofstream out("dfs.out");
void dfs(int node){
vis[node] = true;
for (auto neigh : graph[node]){
if(!vis[neigh])
dfs(neigh);
}
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
int n, m, x, y, cnt = 0;
cin >> n >> m;
graph.resize(n + 1);
vis.resize(n + 1, false);
while(m--){
cin >> x >> y;
graph[x].push_back(y);
graph[y].push_back(x);
}
for(int i = 1; i <= n; ++i){
if(!vis[i]){
dfs(i);
cnt++;
}
}
cout << cnt;
}