-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunion_find.cpp
56 lines (48 loc) · 995 Bytes
/
union_find.cpp
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
55
56
#include "union_find.h"
#include <iostream>
UnionFind :: UnionFind(int _n) {
n = _n;
num_cc = n;
for (int i = 0; i < n; i++){
connected.push_back(i);
}
}
int UnionFind :: parent(int node){
int predecessor = connected[node];
int ans;
if (predecessor != node){
ans = parent(predecessor);
connected[node] = ans;
}
else{
ans = node;
}
return ans;
}
int UnionFind :: thread_safe_parent(int node){
int predecessor = connected[node];
if (predecessor != node){
return parent(predecessor);
}
return node;
}
void UnionFind :: join(int node1, int node2){
int p1 = parent(node1);
int p2 = parent(node2);
if (p1 != p2){
num_cc -= 1;
connected[p2] = p1;
}
}
bool UnionFind :: query(int node1, int node2){
return parent(node1) == parent(node2);
}
bool UnionFind :: thread_safe_query(int node1, int node2){
return thread_safe_parent(node1) == thread_safe_parent(node2);
}
int UnionFind :: get_num_cc(){
return num_cc;
}
UnionFind::~UnionFind() {
connected.clear();
}