Advertisement
Guest User

Untitled

a guest
May 24th, 2019
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.27 KB | None | 0 0
  1. #include <array>
  2. #ifndef UNION_FIND_ALEJANDROGH97_DISJOINT_SET_H
  3. #define UNION_FIND_ALEJANDROGH97_DISJOINT_SET_H
  4.  
  5. template <int N,class Container>
  6.  
  7. class disjoint_set{
  8.  
  9. private:
  10.  
  11.     Container root;
  12.     Container rank;
  13.  
  14. public:
  15.  
  16.     int getRank(int n){
  17.         return rank[n];
  18.     }
  19.  
  20.     int getRoot(int n){
  21.         return root[n];
  22.     }
  23.  
  24.     disjoint_set():root(N*N),rank(N*N) {
  25.  
  26.         for (int i = 0; i < N*N; ++i) {
  27.             root[i]=i;
  28.             rank[i]=0;
  29.         }
  30.     }
  31.     int find(int num) {
  32.         if (num == root[num]){
  33.             return num;
  34.         }
  35.         else{
  36.             root[num] = find(root[num]);
  37.             return root[num];
  38.         }
  39.  
  40.     }
  41.  
  42.     bool same(int vert1, int vert2) {
  43.         int temp1 = find(vert1);
  44.         int temp2 = find(vert2);
  45.  
  46.         return temp1==temp2;
  47.     }
  48.  
  49.     void join(int vert1, int vert2) {
  50.         int root1 = find(vert1);
  51.         int root2 = find(vert2);
  52.  
  53.         if(rank[root1]>rank[root2]){
  54.             root[root2]=root1;
  55.         }
  56.         else{
  57.             root[root1] = root2;
  58.             if(rank[root1] == rank[root2]) {
  59.                 rank[root2]++;
  60.             }
  61.         }
  62.     }
  63. };
  64.  
  65.  
  66. template <int N>
  67. class disjoint_set<N,std::array<int,N>>{
  68.  
  69. private:
  70.  
  71.     std::array<int,N*N> root;
  72.     std::array<int,N*N> rank;
  73.  
  74. public:
  75.  
  76.     int getRank(int n){
  77.         return rank[n];
  78.     }
  79.  
  80.     int getRoot(int n){
  81.         return root[n];
  82.     }
  83.  
  84.      disjoint_set(){
  85.  
  86.         for (int i = 0; i < N*N; ++i) {
  87.             root[i]=i;
  88.             rank[i]=0;
  89.         }
  90.     }
  91.  
  92.     int find(int num) {
  93.         if (num == root[num]){
  94.             return num;
  95.         }
  96.         else{
  97.             root[num] = find(root[num]);
  98.             return root[num];
  99.         }
  100.  
  101.     }
  102.  
  103.     bool same(int vert1, int vert2) {
  104.         int temp1 = find(vert1);
  105.         int temp2 = find(vert2);
  106.  
  107.         return temp1==temp2;
  108.     }
  109.  
  110.     void join(int vert1, int vert2) {
  111.         int root1 = find(vert1);
  112.         int root2 = find(vert2);
  113.  
  114.         if(rank[root1]>rank[root2]){
  115.             root[root2]=root1;
  116.         }
  117.         else{
  118.             root[root1] = root2;
  119.             if(rank[root1] == rank[root2]) {
  120.                 rank[root2]++;
  121.             }
  122.         }
  123.     }
  124. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement