This documentation is automatically generated by competitive-verifier/competitive-verifier
#include "src/DataStructure/UnionFind_Potentialized_Undoable.hpp"
UnionFind_Potentialized_Undoable<weight_t>
クラスundo可能な重み(ポテンシャル)付きUnion-Find.
各要素 $u$ にポテンシャル $p(u)$ ( weight_t
型 )を持たせ,同じ集合に属する任意の2要素 $u,v$ についてポテンシャルの差分 $p(v)-p(u)$ を計算できるようにする.
weight_t
型は weight_t()
,operator+(a,b)
,operator-(a)
,operator-(a,b)
によって群になれば使える.
weight_t=bool
のときは $\mathbb{F}_2$ の意味で使われる.
非可換な群でも動く.その場合計算順序に注意.
unite(u,v,w)
は $p(u) = p(v) + w$ すなわち $-p(v)+p(u)=w$ を満たし,diff(u,v)
は $-p(v)+p(u)$ を返す.
状態が変化しなくても unite
を一回呼ぶごとにタイムスタンプが増える.
経路圧縮をしていないため計算量は $\log$ になる.
メンバ関数 | 概要 | 計算量 |
---|---|---|
UnionFind(n) |
コンストラクタ. 要素数 $n$ を渡す. | |
size(u) |
要素 $u$ の属する集合のサイズを返す. | |
leader(u) |
要素 $u$ の属する集合の代表元を返す. | $O(\log n)$ |
connected(u,v) |
要素 $u,v$ が同じ集合に属していれば true を返す. |
$O(\log n)$ |
unite(u,v,w) |
要素 $u,v$ それぞれが属する集合を併合し,ポテンシャルについては $p(u)=p(v)+w$ を満たすように変更する. 矛盾していたら false を返す. |
$O(\log n)$ |
potential(u) |
要素 $u$ のポテンシャル $p(u)$ を返す. | $O(\log n)$ |
diff(u,v) |
$-p(v)+p(u)$ を返す | $O(\log n)$ |
time() |
この関数を読んだ時点でのタイムスタンプを返す.最初は0. | $O(1)$ |
undo() |
直近の unite 操作を無かったことにする. |
$O(1)$ |
rollback(t) |
タイムスタンプ $t$ のときに戻す. | 最悪unite をした回数 |
#pragma once
#include <vector>
#include <algorithm>
#include <cassert>
template <class weight_t> class UnionFind_Potentialized_Undoable {
std::vector<int> par;
std::vector<weight_t> val;
std::vector<std::tuple<int, int, weight_t, int>> his;
int cur;
public:
UnionFind_Potentialized_Undoable(int n): par(n, -1), val(n), his{{-1, -1, weight_t(), 1}}, cur(0) { his.reserve(n + 1); }
int leader(int u) const { return par[u] < 0 ? u : leader(par[u]); }
// -p(v) + p(u) = w
bool unite(int u, int v, weight_t w) {
if constexpr (std::is_same_v<weight_t, bool>) w^= potential(v) ^ potential(u);
else w= potential(v) + w - potential(u);
if (++cur; (u= leader(u)) == (v= leader(v))) return ++std::get<3>(his.back()), w == weight_t();
if (par[v] > par[u]) std::swap(u, v), w= -w;
return his.emplace_back(u, par[u], val[u], 1), par[v]+= par[u], par[u]= v, val[u]= w, true;
}
bool connected(int u, int v) { return leader(u) == leader(v); }
int size(int u) { return -par[leader(u)]; }
weight_t potential(int u) {
if (par[u] < 0) return val[u];
if constexpr (std::is_same_v<weight_t, bool>) return potential(par[u]) ^ val[u];
else return potential(par[u]) + val[u];
}
// -p(v) + p(u)
weight_t diff(int u, int v) {
if constexpr (std::is_same_v<weight_t, bool>) return potential(u) ^ potential(v);
else return -potential(v) + potential(u);
}
int time() const { return cur; }
void undo() {
if (assert(cur > 0), --cur; --std::get<3>(his.back()) == 0) {
auto [u, p, v, _]= his.back();
par[par[u]]-= p, par[u]= p, val[u]= v, his.pop_back();
}
}
void rollback(int t) {
assert(0 <= t), assert(t <= cur);
if (t == cur) return;
for (;;) {
auto &[u, p, v, i]= his.back();
if (cur-= i; cur < t) {
i= t - cur, cur= t;
break;
}
par[par[u]]-= p, par[u]= p, val[u]= v, his.pop_back();
}
}
};
#line 2 "src/DataStructure/UnionFind_Potentialized_Undoable.hpp"
#include <vector>
#include <algorithm>
#include <cassert>
template <class weight_t> class UnionFind_Potentialized_Undoable {
std::vector<int> par;
std::vector<weight_t> val;
std::vector<std::tuple<int, int, weight_t, int>> his;
int cur;
public:
UnionFind_Potentialized_Undoable(int n): par(n, -1), val(n), his{{-1, -1, weight_t(), 1}}, cur(0) { his.reserve(n + 1); }
int leader(int u) const { return par[u] < 0 ? u : leader(par[u]); }
// -p(v) + p(u) = w
bool unite(int u, int v, weight_t w) {
if constexpr (std::is_same_v<weight_t, bool>) w^= potential(v) ^ potential(u);
else w= potential(v) + w - potential(u);
if (++cur; (u= leader(u)) == (v= leader(v))) return ++std::get<3>(his.back()), w == weight_t();
if (par[v] > par[u]) std::swap(u, v), w= -w;
return his.emplace_back(u, par[u], val[u], 1), par[v]+= par[u], par[u]= v, val[u]= w, true;
}
bool connected(int u, int v) { return leader(u) == leader(v); }
int size(int u) { return -par[leader(u)]; }
weight_t potential(int u) {
if (par[u] < 0) return val[u];
if constexpr (std::is_same_v<weight_t, bool>) return potential(par[u]) ^ val[u];
else return potential(par[u]) + val[u];
}
// -p(v) + p(u)
weight_t diff(int u, int v) {
if constexpr (std::is_same_v<weight_t, bool>) return potential(u) ^ potential(v);
else return -potential(v) + potential(u);
}
int time() const { return cur; }
void undo() {
if (assert(cur > 0), --cur; --std::get<3>(his.back()) == 0) {
auto [u, p, v, _]= his.back();
par[par[u]]-= p, par[u]= p, val[u]= v, his.pop_back();
}
}
void rollback(int t) {
assert(0 <= t), assert(t <= cur);
if (t == cur) return;
for (;;) {
auto &[u, p, v, i]= his.back();
if (cur-= i; cur < t) {
i= t - cur, cur= t;
break;
}
par[par[u]]-= p, par[u]= p, val[u]= v, his.pop_back();
}
}
};