-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathpotentialized_unionfind.hpp
33 lines (32 loc) · 1.15 KB
/
potentialized_unionfind.hpp
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
#pragma once
#include <numeric>
#include <utility>
#include <vector>
// Potentialized UnionFind (Weighted UnionFind)
template <class S> struct PotentializedUnionFind {
std::vector<int> par, sz;
std::vector<S> pot;
PotentializedUnionFind(int N = 0) : par(N), sz(N, 1), pot(N) {
std::iota(par.begin(), par.end(), 0);
}
int find(int x) {
if (par[x] != x) {
int r = find(par[x]);
pot[x] = pot[x] + pot[par[x]], par[x] = r;
}
return par[x];
}
bool unite(int s, int t, S rel_diff) {
// Relate s and t by f[t] = f[s] + rel_diff
// Return false iff contradiction happens.
rel_diff = rel_diff + weight(s) + (-weight(t));
if ((s = find(s)) == (t = find(t))) return rel_diff == 0;
if (sz[s] < sz[t]) std::swap(s, t), rel_diff = -rel_diff;
par[t] = s, sz[s] += sz[t], pot[t] = rel_diff;
return true;
}
S weight(int x) { return find(x), pot[x]; }
S diff(int s, int t) { return weight(t) + (-weight(s)); } // return f[t] - f[s]
int count(int x) { return sz[find(x)]; }
bool same(int s, int t) { return find(s) == find(t); }
};