连通性问题

1.快速查找

#include
#define  N   10

int main(void)
{
    int p,q;   //输入的连通对
    int i,temp,a[N];

    for(i=0;i

2.快速并集

#include
#define  N  10

int main(void)
{
     int p,q;
     int i,j,a[N];
     
     for(i=0;i

3.加权快速并集

#include
#define  N  10

int main(void)
{
     int p,q;
     int i,j,a[N],b[N];
     
     for(i=0;i

4.对分路径加权快速并集

#include
#define  N  10

int main(void)
{
     int p,q;
     int i,j,a[N],b[N];
     
     for(i=0;i

5.练习题


#include
#define  N  10

int main(void)
{
     int p,q;
	 int i,temp,a[N],b[N];//访问次数统计

	 for(i=0;i


#include 
#define N 10
   int main(int argc, char *argv[])
  {
      int i,j;
      int p,q;                    /* 输入的连通对 */
      int id[N],a[N];
      for (i = 0; i < N; ++i)
     {
         id[i]=i;        
		 a[i]=0;                //访问次数
     }
     while(scanf("%d %d",&p,&q)==2)
     {
         for(i=p;a[i]=a[i]+1,i!=id[i];i=id[i]);	 //查找找根,访问次数统计
         for(j=q;a[j]=a[j]+1,j!=id[j];j=id[j]);
		
         if(i==j) continue;
         
         a[i]=j;                           //并集
         printf("\n%d-%d\n",p,q);
		
         for (i=0; i < N; ++i)
         {
              printf("%d ",id[i]);
		 }
        printf("\n");

        for (i=0; i < N; ++i)
         {
              printf("%d ",a[i]);
		 }
        printf("\n");

        for (i=0; i < N; ++i)
         {
              printf("%d ",b[i]);
		 }
        printf("\n");
     }
     printf("\n");
     return 0;
  }


#include 
#define N 10
   int main(int argc, char *argv[])
  {
      int i,j;
      int p,q;                    /* 输入的连通对 */
      int id[N],a[N],b[N];
      for (i = 0; i < N; ++i)
     {
         id[i]=i;
		 a[i]=0;
		 b[i]=1;
     }
     while(scanf("%d %d",&p,&q)==2)
     {
         for(i=p;a[i]=a[i]+1,i!=id[i];i=id[i]);	
         for(j=q;a[j]=a[j]+1,j!=id[j];j=id[j]);
		
         if(i==j) continue;

         printf("\n%d-%d\n",p,q);
		 if(b[i]



#include 
#define N 10
   int main(int argc, char *argv[])
  {
      int i,j;
      int p,q;                    /* 输入的连通对 */
      int id[N],a[N],b[N];
      for (i = 0; i < N; ++i)
     {
         id[i]=i;
		 a[i]=0;
		 b[i]=1;
     }
     while(scanf("%d %d",&p,&q)==2)
     {
         for(i=p;a[i]=a[i]+1,i!=id[i];i=id[i])
			 i=id[i];
         for(j=q;a[j]=a[j]+1,j!=id[j];j=id[j])
			 j=id[j];
		
         if(i==j) continue;

         printf("\n%d-%d\n",p,q);
		 if(b[i]







你可能感兴趣的:(算法与数据结构)