#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int father[1100];
int find(int x)
{
return x==father[x]?x:father[x]=find(father[x]);
}
void merge(int x,int y)
{
x=find(x),y=find(y);
if(x!=y)
father[x]=y;
}
int main( )
{
int P,N,M,d[1100],count,i,j,a,b;
scanf("%d",&P);
while(P--)
{
scanf("%d%d",&N,&M);
count=0;
memset(d,0,sizeof(d));
for(i=1;i<=1100;i++)
father[i]=i;
for(i=1;i<=M;i++)
{
scanf("%d%d",&a,&b);
if(find(a)!=find(b))
merge(a,b);
}
for(i=1;i<=N;i++)
d[find(i)]++;
for(i=1;i<=N;i++)
if(d[i]!=0)
count++;
printf("%d\n",count);
}
return 0;
}
转载于:https://www.cnblogs.com/tangcong/archive/2011/04/30/2033612.html