-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsequential_dsu.h
More file actions
45 lines (34 loc) · 857 Bytes
/
Copy pathsequential_dsu.h
File metadata and controls
45 lines (34 loc) · 857 Bytes
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
#ifndef __SEQUENTIAL_DSU_H
#define __SEQUENTIAL_DSU_H
#include "defs.h"
struct SequentialDSU {
u32 size;
u32* parent;
u32* rank;
SequentialDSU(u32 size) : size(size) {
rank = new u32[size];
parent = new u32[size];
for (u32 i = 0; i < size; ++i) parent[i] = i;
}
~SequentialDSU() {
delete[] parent;
delete[] rank;
}
u32 find_root(u32 id) {
while (id != parent[id]) {
id = parent[id];
}
return id;
}
bool same_set(u32 id1, u32 id2) {
return find_root(id1) == find_root(id2);
}
void unite(u32 id1, u32 id2) {
id1 = find_root(id1);
id2 = find_root(id2);
if (rank[id1] < rank[id2]) std::swap(id1, id2);
parent[id2] = id1;
if (rank[id1] == rank[id2]) ++rank[id1];
}
};
#endif