46 explicit dsu(uint64_t n) {
49 for (uint64_t i = 0; i < n; i++) {
56 for (uint64_t i = 0; i < n; i++) {
63 for (uint64_t i = 0; i < n; i++) {
136 vector<uint64_t>
get(uint64_t i) {
137 vector<uint64_t> ans;
140 ans.push_back(
size(i));
177 vector<uint64_t> ans = {1, 4, 3};
178 for (uint64_t i = 0; i < ans.size(); i++) {
179 assert(d.
get(4).at(i) == ans[i]);
181 cout <<
"1st test passed!" <<
endl;
195 vector<uint64_t> ans = {3, 7, 4};
196 for (uint64_t i = 0; i < ans.size(); i++) {
197 assert(d.
get(3).at(i) == ans[i]);
199 cout <<
"2nd test passed!" <<
endl;
Disjoint sets union data structure, class based representation.
vector< uint64_t > get(uint64_t i)
prints the minimum, maximum and size of the set to which i belongs to
dsu(uint64_t n)
contructor for initialising all data members.
uint64_t findSet(uint64_t i)
Method to find the representative of the set to which i belongs to, T(n) = O(1)
uint64_t size(uint64_t i)
A utility function that returns the size of the set to which i belongs to.
vector< uint64_t > minElement
minimum of each set to which i belongs to
vector< uint64_t > p
keeps track of the parent of ith element
vector< uint64_t > maxElement
maximum of each set to which i belongs to
vector< uint64_t > depth
tracks the depth(rank) of i in the tree
bool isSame(uint64_t i, uint64_t j)
A utility function which check whether i and j belongs to same set or not.
uint64_t get_max(uint64_t i)
A utility function that returns the max element of the set to which i belongs to.
void UnionSet(uint64_t i, uint64_t j)
Method that combines two disjoint sets to which i and j belongs to and make a single set having a com...
vector< uint64_t > setSize
size of each chunk(set)
uint64_t get_min(uint64_t i)
A utility function that returns the min element of the set to which i belongs to.
static void test2()
Self-implementations, 2nd test.
static void test1()
Self-test implementations, 1st test.