就机会复习一下并查集…
1: #include<iostream>
2: #include<string.h>
3: using namespace std;
4: const int maxn=1001;
5: int f[maxn];
6: int n,m,tot=0;
7:
8: int find(int x)
9: {
10: int i=x;
11: while (f[i]!=0) i=f[i];
12: return i;
13: }
14:
15: int main()
16: {
17: memset(f,0,sizeof(f));
18: cin >> n >> m;
19: int prex,prey,fx,fy;
20: for (int i=0;i<n;i++){
21: cin >> prex >> prey;
22: fx=find(prex);
23: fy=find(prey);
24: fx==fy?tot++:f[fx]=fy;
25: }
26: cout << tot << endl;
27: return 0;
28: }