#include <bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, k, current1, current2, team_num;
int group[1000000000] = { 0 };
while (cin >> n >> k) {
team_num = 1;
for (int i = 0; i < k; i++) {
cin >> current1 >> current2;
if (group[current1] == 0) {
if (group[current2] == 0) {
group[current1] = team_num;
group[current2] = team_num;
team_num++;
}
else {
group[current1] = group[current2];
}
}
else {
if (group[current2] == 0) {
group[current2] = group[current1];
}
}
}
cout << team_num - 1 << '\n';
memset(group, 0, n * 4);
}
}
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, k, current1, current2, team_num;
int group[1000000000] = { 0 };
while (cin >> n >> k) {
team_num = 1;
for (int i = 0; i < k; i++) {
cin >> current1 >> current2;
if (group[current1] == 0) {
if (group[current2] == 0) {
group[current1] = team_num;
group[current2] = team_num;
team_num++;
}
else {
group[current1] = group[current2];
}
}
else {
if (group[current2] == 0) {
group[current2] = group[current1];
}
}
}
cout << team_num - 1 << '\n';
memset(group, 0, n * 4);
}
}
應該是陣列太大了