#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int visited[1000];
vector<int> c[1000];
void counting_is_good(int i) {
queue<int> q;
q.push(i);
visited[i] = true;
while (!q.empty()) {
int cc = q.front();
q.pop();
for (int i = 0; i < c[cc].size(); i++) {
int next = c[cc][i];
if (!visited[next]) {
visited[next] = 1;
q.push(next);
}
}
}
}
int main() {
int cnt = 0;
int n, m;
cin >> n >> m;
for (int i = 0; i < m; i++) {
int arr, raa;
cin >> arr >> raa;
c[arr].push_back(raa);
c[raa].push_back(arr);
}
for (int i = 1;i <= n; i++) {
if (!visited[i]) {
counting_is_good(i);
cnt++;
}
}
cout << cnt;
return 0;
}
// _____________
// ___---- ----___
// | |
// | ---| --- |
// | | | |
// | | --- |
// | | | |
// | ------- ----- |
// | |_____________________| |
// | | | |
To embed this project on your website, copy the following code and paste it into your website's HTML: