题意:
判断连通分支数
题解:
并查集
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<string>
#include<cstring>
#include<vector>
#include<functional>
#include<utility>
#include<set>
#include<map>
#include<cmath>using namespace std;const int maxn=1005;
int pa[maxn];
int findset(int x)
{return pa[x]==-1?x:pa[x]=findset(pa[x]);
}void unionset(int x, int y)
{int rx=findset(x);int ry=findset(y);if(rx==ry)return;pa[rx]=ry;
}int main()
{int t;cin>>t;while(t--){int n,m;cin>>n>>m;memset(pa,-1,sizeof(pa));while(m--){ int x,y;scanf("%d%d",&x,&y);unionset(x,y);}int ans=0;for(int i=1;i<=n;i++)if(pa[i]<0)ans++;cout<<ans<<endl;}}