-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunionfind.cpp
More file actions
43 lines (40 loc) · 1.06 KB
/
Copy pathunionfind.cpp
File metadata and controls
43 lines (40 loc) · 1.06 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
#include "unionfind.h"
using namespace std;
// constructor initializes parent and rank arrays
UnionFind::UnionFind(int n)
: parent(n + 1), rankv(n + 1, 0)
{
for (int i = 0; i <= n; ++i)
parent[i] = i; // initially, each element is its own parent (self root)
}
// find the root (representative) of a set with path compression
int UnionFind::find(int x)
{
if (parent[x] != x) // if x is not its own parent, recursively find and compress
parent[x] = find(parent[x]);
return parent[x];
}
// merge two sets (union by rank)
void UnionFind::unite(int a, int b)
{
int ra = find(a);
int rb = find(b);
// attach smaller tree under larger one
if (ra == rb)
return;
if (rankv[ra] < rankv[rb])
parent[ra] = rb;
else if (rankv[ra] > rankv[rb])
parent[rb] = ra;
else
{
// if ranks equal, choose one as root and increment its rank
parent[rb] = ra;
rankv[ra]++;
}
}
// check if two elements belong to the same set
bool UnionFind::same(int a, int b)
{
return find(a) == find(b);
}