版权声明:wywwzjj https://blog.csdn.net/weixin_42348709/article/details/84312185
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int pa[N];
int findRoot(int x) {
return pa[x] != x ? pa[x] = findRoot(pa[x]) : x;
}
void Union(int x, int y) {
int a = findRoot(x);
int b = findRoot(y);
pa[a] = b;
}
int main(){
ios::sync_with_stdio(0);cin.tie(0);
//freopen("D:\\i.txt","r",stdin);
int n, m; cin>>n>>m;
for(int i=1;i<=n;i++) pa[i] = i;
int x, y;
while(m--) {
cin>>x>>y;
Union(x, y);
}
for(int i=1;i<=n;i++) findRoot(i);
set<int> st;
for(int i=1;i<=n;i++)
st.insert(pa[i]);
cout<<st.size()<<endl;
return 0;
}