题目链接:How Many Tables
题目大意:有一堆乱七八糟的朋友关系,然后问你一共有多少个朋友圈
题目思路:并查集裸题,直接写就好了
#include <bits/stdc++.h>
using namespace std;
int t,a,b,m,n,father[1005];
int Find(int x){
if(x == father[x]) return x;
return father[x] = Find(father[x]);
}
void join(int x,int y){
int p = Find(x),q = Find(y);
if(p!=q) father[p] = q;
}
int main(){
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
int Set = n;
for(int i = 0;i <= n;i++)
father[i] = i;
while(m--){
scanf("%d%d",&a,&b);
if(Find(a) != Find(b)) join(a,b),Set--;
}
printf("%d\n",Set);
}
return 0;
}