HDU 1213

来源:互联网 发布:猜数字游戏c语言 编辑:程序博客网 时间:2024/05/21 06:57
 
#include<stdio.h>int find(int *a,int x){if(a[x]==x)return x;elsereturn find(a,a[x]);}void merge(int *a,int x,int y){a[x]=y;}int main(){int cas,n,m,count,i,x,y,father[1001];scanf("%d",&cas);while(cas--){scanf("%d%d",&n,&m);for(i=0;i<=n;i++)  //i<=nfather[i]=i;while(m--){scanf("%d%d",&x,&y);if(find(father,x)!=find(father,y))merge(father,find(father,x),find(father,y));}for(i=1,count=0;i<=n;i++) //i从1开始,不能从0开始{if(find(father,i)==i)count++;}printf("%d\n",count);}return 0;}

原创粉丝点击