纯粹的并查集
#include <iostream>
#include <string>
#include <algorithm>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int father[1111];
int get_fa(int x)
{
if(x != father[x]) father[x] = get_fa(father[x]);
return father[x];
}
int main()
{
int t;
while(scanf("%d", &t) != EOF)
{
while(t--)
{
int n, m;
scanf("%d%d", &n, &m);
for(int i = 0; i <= n; i++)
father[i] = i;
while(m--)
{
int a, b;
scanf("%d%d", &a, &b);
a = get_fa(a), b = get_fa(b);
father[b] = a;
}
int cnt = 0;
for(int i = 1; i <= n; i++)
if(father[i] == i)
cnt++;
printf("%d\n", cnt);
}
}
return 0;
}