-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisjoint_union_set_4.cpp
More file actions
42 lines (35 loc) · 1.01 KB
/
disjoint_union_set_4.cpp
File metadata and controls
42 lines (35 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Disjoint set Union
// https://cp-algorithms.com/data_structures/disjoint_set_union.html#storing-the-dsu-explicitly-in-a-set-list-applications-of-this-idea-when-merging-various-data-structures
// application: Storing the DSU explicitly in a set list
#include <bits/stdc++.h>
#define MAXN 10
std::vector<int> lst[MAXN]
int parent[MAXN];
// creates a new set consisting of the new element v
void make_set(int v) {
lst[v] = std::vector<int>(1, v);
poarent[v] = v;
}
// find_set(v) - returns the representative (also called leader) of the set that contains the element v
// Path compression optimization
int find_set(int v) {
return parent[v];
}
void union_sets(int a, int b) {
a = find_set(a);
b = find_set(b);
if (a != b) {
if (lst[a].size() < lst[b].size())
std::swap(a, b);
while(!lst[b].empty()) {
int v = lst[b].back();
lst[b].pop_back();
parebt[v] = a;
lst[a].push_back(v);
}
}
}
int main()
{
return 0;
}