并查集超级水题
#include<iostream>
#include<queue>
#include<stack>
#include<cstdlib>
#include<memory.h>
using namespace std;
int map[1001];
int book[1001];
int gen(int x)
{
while(x!=map[x])
{
x=map[x];
}
return x;
}
void hebing(int a,int b)
{
int a1=gen(a);
int b1=gen(b);
if(a1!=b1)
{
map[a1]=b1;
}
}
int main()
{
int T;
cin>>T;
while(T--)
{
memset(book,0,sizeof(book));
int n,m;
cin>>n>>m;
for(int a=1;a<=n;a++)map[a]=a;
for(int sd=1;sd<=m;sd++)
{
int s,d;
cin>>s>>d;
hebing(s,d);
}
for(int a=1;a<=n;a++)
{
book[gen(a)]=1;
}
int sum=0;
for(int a=1;a<=n;a++)
{
if(book[a]==1)sum++;
}
cout<<sum<<endl;
}
return 0;
}