题目链接:
题意:并查集简单运用
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
#define maxn 1010
int F[maxn];
int vis[maxn];
int find(int x)
{
if(F[x] == -1) return x;
return F[x] = find(F[x]);
}
void bing(int x, int y)
{
int p1 = find(x);
int p2 = find(y);
if(p1 != p2) F[p1] = p2;
}
int main()
{
int t;
cin>>t;
while(t--){
int n, m;
int a, b;
cin >> n >> m;
memset(vis, -1, sizeof(vis));
for(int i = 0; i <= n; i++)
F[i] = -1;
for(int i = 0; i < m; i++){
cin >> a >> b;
bing(a, b);
}
int res = 0;
for(int i = 1; i <= n; i++){
int num = find(i);
if(vis[num] == -1){
res ++;
vis[num] = true;
}
}
cout << res << endl;
}
return 0;
}