Othello
 All Classes Files Functions Variables Macros
disjointset.h
Go to the documentation of this file.
1 #pragma once
2 #include <vector>
3 using namespace std;
10 class DisjointSet {
11  vector<int32_t> *fa = NULL;
12 public:
13  uint32_t getfa(int i) {
14  if ((*fa)[i] < 0) (*fa)[i] = i;
15  else if ((*fa)[i]!=i)
16  (*fa)[i] = getfa((*fa)[i]);
17  return (*fa)[i];
18  }
20  void finish() {
21  fa->clear();
22  delete fa;
23  fa = NULL;
24  }
25  void setLength(int n) {
26  if (fa == NULL)
27  fa = new vector<int32_t> (n,-1);
28  else {
29  fa->resize(0); fa->resize(n,-1); }
30  }
32  void clear() {
33  for (auto a : *fa)
34  a = -1;
35  }
36  void merge(int a, int b) {
37  if (a==0) swap(a,b); //a!=0
38  (*fa)[getfa(b)] = getfa(a);
39  }
40  bool sameset(int a, int b) {
41  return getfa(a)==getfa(b);
42  }
43  bool isroot(int a) {
44  return ((*fa)[a]==a);
45  }
47  bool resize(int n) {
48  while (fa->size()<n)
49  fa->push_back(-1);
50 
51  }
52 };
bool resize(int n)
add new keys, so that the total number of elements equal to n.
Definition: disjointset.h:47
void finish()
Release the memory to save some space.
Definition: disjointset.h:20
void clear()
re-initilize the disjoint sets.
Definition: disjointset.h:32
Disjoint Set data structure. Helps to test the acyclicity of the graph during construction.
Definition: disjointset.h:10