hdu1232
强连通分量计数
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
int root[1010];
int n,m;
int findroot(int index){
if(root[index] == -1)
return index;
else
return findroot(root[index]);
}
int main(){
int i;
int j;
int bufx,bufy;
freopen("in_kruskal.txt","r",stdin);
freopen("out.txt","w",stdout);
while(scanf("%d",&n) && n != 0){
scanf("%d",&m);
memset(root,-1,sizeof(root));
while(m--){
scanf("%d %d",&bufx,&bufy);
int bufroot1 = findroot(bufx-1);
int bufroot2 = findroot(bufy-1);
if(bufroot1 != bufroot2)
root[bufroot1] = bufroot2;
}
int count = 0;
for(i = 0 ; i < n ; i++){
if(root[i] == -1)
count++;
}
printf("%d\n",count-1);
}
return 0;
}

浙公网安备 33010602011771号