
查环操作,裸题。一次AC。
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <string>
#include <cstdio>
#include <algorithm>
#include <numeric>
using namespace std; const int maxnn = 1e4 + ;
int cnt = , father[maxnn]; int getFather (int x) {
if (father[x] != x) {
father[x] = getFather (father[x]);
}
return father[x];
} void Union (int p, int q) {
int x = getFather (p);
int y = getFather (q);
if (x != y) {
father[y] = x;
} else {
cnt ++;
}
} int main () {
int n, x;
while (cin >> n >> x) {
for (int i = ; i < x; ++ i) {
father[i] = i;
}
cnt = ;
for (int i = ;i < x; ++ i) {
int p, q; scanf("%d%d", &p, &q);
Union (p, q);
} /*for (int i = 0; i < n; ++ i) {
if (getFather (i) == i) {
ans ++;
}
}*/
cout << cnt << endl;
}
return ;
}