连通性问题快速查找算法

#include <stdio.h>
#include <stdlib.h>

#define N 10
int main(int argc, char *argv[])
{
    int count;
    int i=0,p,q,t,id[N];
    for (i=0; i < N; ++i)
    {
        id[i]=i;
    }
    while(scanf("%d %d",&p,&q)==2)
    {
        if(id[p]==id[q]) continue;
        count=0;
        for(t=id[p],i=0;i<N;++i)
        {
            if (t==id[i])
            {
                count++;
                id[i]=id[q];
            }
        }
        for (i=0; i < N; ++i)
        {
            printf("%d ",id[i]);
        }
        printf("\n\n");
    }
    system("pause");
    return 0;
}

 

posted @ 2013-05-21 10:53  stdtrekman  阅读(256)  评论(0编辑  收藏  举报