zoukankan      html  css  js  c++  java
  • 并查集

    /*并查集*/
    #include<stdio.h>
    
    int *a;
    int *sz;
    int count;  //the number of connected component
    
    //union two connected components with weights
    void union_two_points(int p, int q)
    {
      int i = root(p);
      int j = root(q);
      if(i == j) return;
      if(sz[i] < sz[j]) 
      {
         a[i] = j;
         sz[j] += sz[i];
      }
      else
      {
         a[j] = i;
         sz[i] += sz[j];
      }
      count--;
    }
    
    
    //test if p and q are connected
    int connected(int p, int q)
    {
      return root[p] == root[q];
    }
    
    
    //find the root point
    int root(int p)
    {
       while(p != a[p])
       {
          p = a[p];
       }
       return p;
    }
    
    
    int main()
    {
       int T;
       printf("Please input the number of points:");
       scanf("%d",&T);
    
       count = T;
       a = (int*)malloc(sizeof(int)*T);
       sz = (int*)malloc(sizeof(int)*T);
    
       memset(sz,1,T*sizeof(int));      //set the size array
       //initial the array 
       for(int i=0;i<T;i++)
       {
          a[i] = i;
       }
    
       /*
       operation
       */
    }
  • 相关阅读:
    基本排序
    mysql安装
    函数和方法
    COOKIE 与 SESSION
    django---入门
    django模板继承
    2017-11-14
    Django---模板层(template)
    Dajngo---model基础
    Django--基础篇
  • 原文地址:https://www.cnblogs.com/dzy521/p/9552893.html
Copyright © 2011-2022 走看看