競技プログラミング用の C++ ライブラリ
#define PROBLEM "https://judge.yosupo.jp/problem/persistent_unionfind"
#include "../../../src/data_structures/persistent-unionfind.hpp"
#include <iostream>
#include <vector>
int main() {
int n, q;
std::cin >> n >> q;
// uf[i + 1] がクエリ i の後の版、uf[0] が初期の版
std::vector<PersistentUnionFind> uf(q + 1);
uf[0] = PersistentUnionFind(n);
for (int i = 0; i < q; i++) {
int t, k, u, v;
std::cin >> t >> k >> u >> v;
const PersistentUnionFind& prev = uf[k + 1];
if (t == 0) {
uf[i + 1] = prev.merge(u, v);
} else {
uf[i + 1] = prev;
std::cout << prev.same(u, v) << '\n';
}
}
}#line 1 "test/yosupo/data_structure/persistent-unionfind.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/persistent_unionfind"
#line 2 "src/data_structures/persistent-unionfind.hpp"
/**
* @file persistent-unionfind.hpp
* @brief 永続UnionFind
*/
#include <cassert>
#include <utility>
#include <vector>
#line 2 "src/data_structures/persistent-array.hpp"
/**
* @file persistent-array.hpp
* @brief 永続配列
*/
#include <array>
#line 10 "src/data_structures/persistent-array.hpp"
#include <memory>
#include <ostream>
#line 14 "src/data_structures/persistent-array.hpp"
/**
* @brief 永続配列
*
* 更新すると新しい版を返し、元の版は変わらない。同じ配列から作った版はノードを共有する
* 例:
* PersistentArray<int> a0(n, -1);
* auto a1 = a0.set(3, 10); // a0 はそのまま
* a1[3]; // 10
*
* @tparam T 要素の型
*/
template <typename T>
class PersistentArray {
// すべての版のノードを持つ。ノードは削除しない
struct Pool {
std::vector<std::array<int, 2>> child; // 内部ノードの子 (最下段の内部ノードでは葉の番号)
std::vector<T> leaf;
int make_leaf(const T& x) {
leaf.push_back(x);
return static_cast<int>(leaf.size()) - 1;
}
int make_node(int l, int r) {
child.push_back({l, r});
return static_cast<int>(child.size()) - 1;
}
};
std::shared_ptr<Pool> pool; // 要素数 0 なら nullptr
long long n;
int height, root; // height = 0 なら root は葉の番号
PersistentArray(std::shared_ptr<Pool> pool, long long n, int height, int root) : pool(std::move(pool)), n(n), height(height), root(root) {}
void init_height() {
height = 0;
while ((1LL << height) < n) height++;
}
// t ([a, a + 2^h)) の [l, r) を u に置き換える
int copy_rec(int t, int u, long long a, int h, long long l, long long r) const {
if (r <= a || a + (1LL << h) <= l) return t;
if (l <= a && a + (1LL << h) <= r) return u;
int lc = copy_rec(pool->child[t][0], pool->child[u][0], a, h - 1, l, r);
int rc = copy_rec(pool->child[t][1], pool->child[u][1], a + (1LL << (h - 1)), h - 1, l, r);
return pool->make_node(lc, rc);
}
int build(const std::vector<T>& v, long long a, int h) {
if (h == 0) return pool->make_leaf(a < n ? v[a] : T());
int l = build(v, a, h - 1);
int r = build(v, a + (1LL << (h - 1)), h - 1);
return pool->make_node(l, r);
}
public:
/**
* @brief コンストラクタ
*
* 同じ値の部分木は共有するので、n が大きくても O(log n) で作れる
*
* @param n 要素数
* @param x 初期値
*/
explicit PersistentArray(long long n = 0, const T& x = T()) : pool(nullptr), n(n), height(0), root(-1) {
assert(0 <= n);
if (n == 0) return;
pool = std::make_shared<Pool>();
init_height();
root = pool->make_leaf(x);
for (int h = 0; h < height; h++) root = pool->make_node(root, root);
}
/**
* @brief コンストラクタ
*
* @param v 初期の要素
*/
explicit PersistentArray(const std::vector<T>& v) : pool(nullptr), n(v.size()), height(0), root(-1) {
if (n == 0) return;
pool = std::make_shared<Pool>();
init_height();
root = build(v, 0, height);
}
/**
* @brief 要素数を返す
*/
long long size() const { return n; }
/**
* @brief 指定された要素の値を返す
*
* @param k インデックス
* @return T 値
*/
T get(long long k) const {
assert(0 <= k && k < n);
int t = root;
for (int h = height - 1; h >= 0; h--) t = pool->child[t][k >> h & 1];
return pool->leaf[t];
}
/**
* @brief 指定された要素の値を返す
*
* @param k インデックス
* @return T 値
*/
T operator[](long long k) const { return get(k); }
/**
* @brief 指定された要素の値をxにした新しい版を返す
*
* @param k インデックス
* @param x 新しい値
* @return PersistentArray 新しい版 (自身は変わらない)
*/
PersistentArray set(long long k, const T& x) const {
assert(0 <= k && k < n);
std::vector<int> path(height);
int t = root;
for (int h = height - 1; h >= 0; h--) {
path[h] = t;
t = pool->child[t][k >> h & 1];
}
t = pool->make_leaf(x);
for (int h = 0; h < height; h++) {
std::array<int, 2> c = pool->child[path[h]];
c[k >> h & 1] = t;
t = pool->make_node(c[0], c[1]);
}
return PersistentArray(pool, n, height, t);
}
/**
* @brief [l, r)の区間を別の版の値にした新しい版を返す
*
* @param other 写す元の版 (自身と同じ配列から作ったもの)
* @param l 半開区間の開始
* @param r 半開区間の終端 0<=l<=r<=n
* @return PersistentArray 新しい版 (自身も other も変わらない)
*/
PersistentArray copy(const PersistentArray& other, long long l, long long r) const {
assert(pool == other.pool);
assert(0 <= l && l <= r && r <= n);
if (l == r) return *this;
return PersistentArray(pool, n, height, copy_rec(root, other.root, 0, height, l, r));
}
/**
* @brief 内容を出力する
*
* @tparam CharT 出力ストリームの文字型
* @tparam Traits 出力ストリームの文字型特性
* @param os 出力ストリーム
* @param rhs 永続配列
* @return std::basic_ostream<CharT, Traits>& 出力ストリーム
*/
template <class CharT, class Traits>
friend std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os, const PersistentArray& rhs) {
for (long long i = 0; i < rhs.n; i++) {
if (i != 0) os << CharT(' ');
os << rhs[i];
}
return os;
}
/**
* @brief すべての要素を並べた列を返す
*/
std::vector<T> to_vector() const {
std::vector<T> res(n);
for (long long i = 0; i < n; i++) res[i] = get(i);
return res;
}
};
#line 2 "src/data_structures/unionfind.hpp"
/**
* @file unionfind.hpp
* @brief UnionFind
*/
#include <algorithm>
#line 12 "src/data_structures/unionfind.hpp"
namespace internal_unionfind {
/**
* @brief 各要素の代表元から、連結成分ごとの要素のリストを作る
* @param leader 要素 a の代表元を返す関数
*/
template <typename F>
std::vector<std::vector<long long>> groups(long long n, F leader) {
std::vector<long long> leader_buf(n), group_size(n);
for (long long i = 0; i < n; i++) {
leader_buf[i] = leader(i);
group_size[leader_buf[i]]++;
}
std::vector<std::vector<long long>> result(n);
for (long long i = 0; i < n; i++) {
result[i].reserve(group_size[i]);
}
for (long long i = 0; i < n; i++) {
result[leader_buf[i]].push_back(i);
}
result.erase(std::remove_if(result.begin(), result.end(), [&](const std::vector<long long>& v) { return v.empty(); }), result.end());
return result;
}
} // namespace internal_unionfind
/**
* @brief 無向グラフに対して「辺の追加」、「2頂点が連結かの判定」をする
*/
class UnionFind {
protected:
long long _n;
// 負ならサイズ、非負なら親 (メモリを抑えるため int で持つ)
std::vector<int> parent_or_size;
public:
/**
* @brief merge の戻り値
*
* if (uf.merge(a, b)) で新しく統合したかを判定でき、
* auto [r, merged] = uf.merge(a, b); で代表元も受け取れる
*/
struct MergeResult {
long long leader; //!< 統合後の代表元
bool merged; //!< 新しく統合したか (もともと同じ連結成分なら false)
explicit operator bool() const { return merged; }
};
UnionFind() : _n(0) {}
explicit UnionFind(long long n) : _n(n), parent_or_size(n, -1) {}
/**
* @brief 辺(a,b)を足す
* @return 統合後の代表元と、新しく統合したか
*/
MergeResult merge(long long a, long long b) {
assert(0 <= a && a < _n);
assert(0 <= b && b < _n);
long long x = leader(a), y = leader(b);
if (x == y) return {x, false};
if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y);
parent_or_size[x] += parent_or_size[y];
parent_or_size[y] = static_cast<int>(x);
return {x, true};
}
/**
* @brief 頂点a,bが連結かどうか
*/
bool same(long long a, long long b) {
assert(0 <= a && a < _n);
assert(0 <= b && b < _n);
return leader(a) == leader(b);
}
/**
* @brief 頂点aの属する連結成分の代表元
*/
long long leader(long long a) {
assert(0 <= a && a < _n);
if (parent_or_size[a] < 0) return a;
int x = static_cast<int>(a);
while (parent_or_size[x] >= 0) {
x = parent_or_size[x];
}
while (parent_or_size[a] >= 0) {
int t = parent_or_size[a];
parent_or_size[a] = x;
a = t;
}
return x;
}
/**
* @brief 頂点aの属する連結成分のサイズ
*/
long long size(long long a) {
assert(0 <= a && a < _n);
return -parent_or_size[leader(a)];
}
/**
* @brief グラフを連結成分に分け、その情報を返す
* @return 「一つの連結成分の頂点番号のリスト」のリスト
*/
std::vector<std::vector<long long>> groups() {
return internal_unionfind::groups(_n, [&](long long a) { return leader(a); });
}
};
#line 14 "src/data_structures/persistent-unionfind.hpp"
/**
* @brief 永続UnionFind
*
* 辺を追加すると新しい版を返し、元の版は変わらない。union by size だけを行い、経路圧縮はしない
* 例:
* PersistentUnionFind uf0(n);
* auto uf1 = uf0.merge(0, 1); // uf0 はそのまま
* uf1.same(0, 1); // true
*/
class PersistentUnionFind {
long long _n;
// 負ならサイズ、非負なら親 (メモリを抑えるため int で持つ)
PersistentArray<int> parent_or_size;
PersistentUnionFind(long long n, PersistentArray<int> parent_or_size) : _n(n), parent_or_size(std::move(parent_or_size)) {}
public:
PersistentUnionFind() : _n(0) {}
explicit PersistentUnionFind(long long n) : _n(n), parent_or_size(n, -1) {}
/**
* @brief 辺(a,b)を足した新しい版を返す
* @return PersistentUnionFind 新しい版 (自身は変わらない。もともと連結なら同じ版)
*/
PersistentUnionFind merge(long long a, long long b) const {
assert(0 <= a && a < _n);
assert(0 <= b && b < _n);
long long x = leader(a), y = leader(b);
if (x == y) return *this;
int sx = parent_or_size[x], sy = parent_or_size[y];
if (-sx < -sy) std::swap(x, y);
return PersistentUnionFind(_n, parent_or_size.set(x, sx + sy).set(y, static_cast<int>(x)));
}
/**
* @brief 頂点a,bが連結かどうか
*/
bool same(long long a, long long b) const {
assert(0 <= a && a < _n);
assert(0 <= b && b < _n);
return leader(a) == leader(b);
}
/**
* @brief 頂点aの属する連結成分の代表元
*/
long long leader(long long a) const {
assert(0 <= a && a < _n);
while (true) {
int p = parent_or_size[a];
if (p < 0) return a;
a = p;
}
}
/**
* @brief 頂点aの属する連結成分のサイズ
*/
long long size(long long a) const {
assert(0 <= a && a < _n);
return -parent_or_size[leader(a)];
}
/**
* @brief グラフを連結成分に分け、その情報を返す
* @return 「一つの連結成分の頂点番号のリスト」のリスト
*/
std::vector<std::vector<long long>> groups() const {
return internal_unionfind::groups(_n, [&](long long a) { return leader(a); });
}
};
#line 4 "test/yosupo/data_structure/persistent-unionfind.test.cpp"
#include <iostream>
#line 7 "test/yosupo/data_structure/persistent-unionfind.test.cpp"
int main() {
int n, q;
std::cin >> n >> q;
// uf[i + 1] がクエリ i の後の版、uf[0] が初期の版
std::vector<PersistentUnionFind> uf(q + 1);
uf[0] = PersistentUnionFind(n);
for (int i = 0; i < q; i++) {
int t, k, u, v;
std::cin >> t >> k >> u >> v;
const PersistentUnionFind& prev = uf[k + 1];
if (t == 0) {
uf[i + 1] = prev.merge(u, v);
} else {
uf[i + 1] = prev;
std::cout << prev.same(u, v) << '\n';
}
}
}