并查集

 

 

实现并查集:

  1 #include <iostream>
  2 #include <vector>
  3 
  4 using namespace std;
  5 
  6 class UnionFindSet {
  7 public:
  8     UnionFindSet(int size) : size_(size) {
  9         this->size_ = size;
 10         this->numGroups_ = size;
 11         this->groupSize_ = new int[size];
 12         this->parent_ = new int[size];
 13 
 14         for (int i = 0; i < size; i++) {
 15             parent_[i] = i;
 16             groupSize_[i] = 1;
 17         }
 18     }
 19     ~UnionFindSet() {
 20         delete this->groupSize_;
 21         delete this->parent_;
 22     }
 23     // 查找某个节点的根节点
 24     int FindRoot(int x) {
 25         int root = x;
 26         while (root != parent_[root]) {
 27             root = parent_[root];
 28         }
 29         // 路径压缩,每个节点的根节点均为root
 30         while (x != root) {
 31             int next = parent_[x];
 32             parent_[x] = root;
 33             x = next;
 34         }
 35         return root;
 36     }
 37     // 判定两个节点是否归属同一组
 38     bool IsConnected(int x, int y) {
 39         return (FindRoot(x) == FindRoot(y));
 40     }
 41     // 获取并查集的元素个数
 42     int GetSize() {
 43         return size_;
 44     }
 45     // 获取并查集组的个数
 46     int GetNumGroups() {
 47         return numGroups_;
 48     }
 49     // 获取x所在组节点个数
 50     int GetGroupSize(int x) {
 51         int root = FindRoot(x);
 52         return groupSize_[root];
 53     }
 54     // 获取组的个数
 55     int GetNumOfGroups() {
 56         return numGroups_;
 57     }
 58     // 合并两个节点所在组
 59     void Union(int x, int y) {
 60         if (IsConnected(x, y)) {
 61             return;
 62         }
 63         int xRoot = FindRoot(x);
 64         int yRoot = FindRoot(y);
 65         if (groupSize_[xRoot] < groupSize_[yRoot]) {
 66             groupSize_[yRoot] += groupSize_[xRoot];
 67             parent_[xRoot] = yRoot;
 68         } else {
 69             groupSize_[xRoot] += groupSize_[yRoot];
 70             parent_[yRoot] = xRoot;
 71         }
 72         numGroups_--;
 73         return;
 74     }
 75 private:
 76     int size_; // 并查集中元素个数
 77     int *groupSize_; // 每组元素个数
 78     int *parent_; // 根节点
 79     int numGroups_; // 并查集中组的个数
 80 };
 81 
 82 int main()
 83 {
 84     UnionFindSet unionFind(6);
 85     // edges = [[1,2], [2,3], [3,4], [1,4], [1,5]]
 86     vector<vector<int>> edges = {{1, 2}, {2, 3}, {3, 4}, {1, 4}, {1, 5}};
 87     for (auto &vec : edges)
 88     {
 89         unionFind.Union(vec[0], vec[1]);
 90     }
 91     for (int i = 1; i <= 5; i++) {
 92         cout << i << "'s root is:= "<< unionFind.FindRoot(i) << endl;
 93     }
 94 
 95     if (unionFind.IsConnected(1, 4)) {
 96         cout << "Detected circle!" << endl;
 97     } else {
 98         cout << "not circle!" << endl;
 99     }
100     cout << "1's group size is:"<< unionFind.GetGroupSize(1) << endl;
101     cout << "5's group size is:"<< unionFind.GetGroupSize(5) << endl;
102     cout << "group size is:"<< unionFind.GetNumGroups() << endl; // group1:(1, 2, 3, 4, 5),group2:(0)
103     system("pause");
104     return 0;
105 }

 

posted @ 2022-04-04 01:18  跳动的休止符  阅读(27)  评论(0)    收藏  举报