This documentation is automatically generated by online-judge-tools/verification-helper
#include "src/data_structure/rollback_union_find.hpp"
通常のUnionFindに加えて,
merge
の操作の取り消しができます.
RollbackUnionFind uf(int n)
計算量
int uf.merge(int a, int b)
辺 $(a, b)$ を足します.
頂点 $a$ と $b$ が連結だった場合はその代表元,非連結だった場合は新たな代表元を返します.
制約
計算量
bool uf.same(int a, int b)
頂点 $a$ と $b$ が連結かどうかを返します.
制約
計算量
int uf.leader(int a)
頂点 $a$ の属する連結成分の代表元を返します.
制約
計算量
int uf.size(int a)
頂点 $a$ の属する連結成分のサイズを返します.
制約
計算量
void uf.undo()
直前の merge
の操作を取り消します.
制約
merge
を行なっていること計算量
int uf.get_state()
merge
を何回行った状態であるかを返します.
計算量
int uf.snapshot()
現在のグラフの状態を記憶します.
複数の状態を記憶することはできません.
最後に呼び出したときの状態のみを記憶します.
計算量
void uf.rollback(int state = -1)
$\mathrm{state = -1}$ のとき,最新の snapshot
で記憶した状態までグラフを巻き戻します.
それ以外のとき, merge
が $\mathrm{state}$ 回行われた状態へ戻します.
制約
merge
が呼び出された階数計算量
#pragma once
#include "../template/template.hpp"
struct RollbackUnionFind {
RollbackUnionFind(const int N)
: n(N), data(N, -1), inner_snap(0) {
}
int merge(const int a, const int b) {
assert(0 <= a and a < n);
assert(0 <= b and b < n);
int x = leader(a), y = leader(b);
history.emplace(x, data[x]);
history.emplace(y, data[y]);
if(x == y) return x;
if(-data[x] < -data[y]) swap(x, y);
data[x] += data[y];
data[y] = x;
return x;
}
bool same(const int a, const int b) const {
assert(0 <= a and a < n);
assert(0 <= b and b < n);
return leader(a) == leader(b);
}
int leader(const int a) const {
assert(0 <= a and a < n);
if(data[a] < 0) return a;
return leader(data[a]);
}
int size(const int a) const {
assert(0 <= a and a < n);
return (-data[leader(a)]);
}
void undo() {
assert((int)history.size() >= 2);
data[history.top().first] = history.top().second;
history.pop();
data[history.top().first] = history.top().second;
history.pop();
}
void snapshot() {
inner_snap = (int)history.size() / 2;
}
int get_state() const {
return (int)history.size() / 2;
}
void rollback(int state = -1) {
if(state == -1) state = inner_snap;
state *= 2;
assert(state <= (int)history.size());
while(state < (int)history.size()) undo();
}
private:
int n;
vector<int> data;
stack<pair<int, int>> history;
int inner_snap;
};
#line 2 "src/template/template.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<long long, long long>;
#define rep(i, a, b) for(long long i = (a); i < (b); ++i)
#define rrep(i, a, b) for(long long i = (a); i >= (b); --i)
constexpr long long inf = 4e18;
struct SetupIO {
SetupIO() {
ios::sync_with_stdio(0);
cin.tie(0);
cout << fixed << setprecision(30);
}
} setup_io;
#line 3 "src/data_structure/rollback_union_find.hpp"
struct RollbackUnionFind {
RollbackUnionFind(const int N)
: n(N), data(N, -1), inner_snap(0) {
}
int merge(const int a, const int b) {
assert(0 <= a and a < n);
assert(0 <= b and b < n);
int x = leader(a), y = leader(b);
history.emplace(x, data[x]);
history.emplace(y, data[y]);
if(x == y) return x;
if(-data[x] < -data[y]) swap(x, y);
data[x] += data[y];
data[y] = x;
return x;
}
bool same(const int a, const int b) const {
assert(0 <= a and a < n);
assert(0 <= b and b < n);
return leader(a) == leader(b);
}
int leader(const int a) const {
assert(0 <= a and a < n);
if(data[a] < 0) return a;
return leader(data[a]);
}
int size(const int a) const {
assert(0 <= a and a < n);
return (-data[leader(a)]);
}
void undo() {
assert((int)history.size() >= 2);
data[history.top().first] = history.top().second;
history.pop();
data[history.top().first] = history.top().second;
history.pop();
}
void snapshot() {
inner_snap = (int)history.size() / 2;
}
int get_state() const {
return (int)history.size() / 2;
}
void rollback(int state = -1) {
if(state == -1) state = inner_snap;
state *= 2;
assert(state <= (int)history.size());
while(state < (int)history.size()) undo();
}
private:
int n;
vector<int> data;
stack<pair<int, int>> history;
int inner_snap;
};