Fu_L's Library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub Fu-L/cp-library

:heavy_check_mark: RollbackUnionFind
(src/data_structure/rollback_union_find.hpp)

RollbackUnionFind

通常のUnionFindに加えて,

ができます.

コンストラクタ

RollbackUnionFind uf(int n)

計算量

merge

int uf.merge(int a, int b)

辺 $(a, b)$ を足します.

頂点 $a$ と $b$ が連結だった場合はその代表元,非連結だった場合は新たな代表元を返します.

制約

計算量

same

bool uf.same(int a, int b)

頂点 $a$ と $b$ が連結かどうかを返します.

制約

計算量

leader

int uf.leader(int a)

頂点 $a$ の属する連結成分の代表元を返します.

制約

計算量

size

int uf.size(int a)

頂点 $a$ の属する連結成分のサイズを返します.

制約

計算量

undo

void uf.undo()

直前の merge の操作を取り消します.

制約

計算量

get_state

int uf.get_state()

merge を何回行った状態であるかを返します.

計算量

snapshot

int uf.snapshot()

現在のグラフの状態を記憶します.

複数の状態を記憶することはできません.
最後に呼び出したときの状態のみを記憶します.

計算量

rollback

void uf.rollback(int state = -1)

$\mathrm{state = -1}$ のとき,最新の snapshot で記憶した状態までグラフを巻き戻します.
それ以外のとき, merge が $\mathrm{state}$ 回行われた状態へ戻します.

制約

計算量

Depends on

Verified with

Code

#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;
};
Back to top page