-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisjoint_set.cpp
More file actions
54 lines (44 loc) · 1.18 KB
/
Copy pathdisjoint_set.cpp
File metadata and controls
54 lines (44 loc) · 1.18 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
43
44
45
46
47
48
49
50
51
52
53
54
#include <bits/stdc++.h>
using namespace std;
// Graph Algorithm for Disjoint Set Union Find
// FIND
int find(int x, vector<int>& parent) {
if (x == parent[x]) return x;
return find(parent[x], parent);
}
// UNION
void unionSet(int x, int z, vector<int>& parent) {
int x_parent = find(x, parent);
int z_parent = find(z, parent);
if (x_parent != z_parent) {
parent[x_parent] = z_parent;
}
}
int main() {
const int N = 10;
vector<int> parent(N);
// Initialize each element with its index as the parent
for (int i = 0; i < N; i++) {
parent[i] = i;
}
// Perform union operations
unionSet(0, 1, parent);
unionSet(2, 3, parent);
unionSet(4, 5, parent);
unionSet(6, 7, parent);
unionSet(8, 9, parent);
unionSet(1, 3, parent); // Combine sets {0, 1, 2, 3}
unionSet(5, 7, parent); // Combine sets {4, 5, 6, 7}
// Print the parent of each element after union operations
cout << "Element: ";
for (int i = 0; i < N; i++) {
cout << i << " ";
}
cout << endl;
cout << "Parent: ";
for (int i = 0; i < N; i++) {
cout << find(i, parent) << " ";
}
cout << endl;
return 0;
}