连通性问题快速并集算法
#include <stdio.h> #include <stdlib.h> #define N 10 void main() { int p,q; int i,j; int id[N]; for(int i=0;i<N;i++) id[i] = i; while(scanf("%d %d ",&p,&q)==2) { for(i=p;i!=id[i];i=id[i]); for(j=q;j!=id[j];j=id[j]); if(i == j) continue; id[i] = j; for(i=0;i<N;i++) printf("%d ",id[i]); printf("\n\n"); } system("pause"); }