This documentation is automatically generated by online-judge-tools/verification-helper
#define PROBLEM "https://judge.yosupo.jp/problem/persistent_unionfind"
#include "../../data_structure/persistent_unionfind.hpp"
int main() {
ll n, q;
cin >> n >> q;
vector<persistent_unionfind<4>> his;
his.emplace_back(n);
for (ll i : rep(q)) {
ll c, k, u, v;
cin >> c >> k >> u >> v, ++k;
if (c == 0) { his.emplace_back(his[k].unite(u, v)); }
if (c == 1) {
cout << his[k].same(u, v) << '\n';
his.emplace_back(1);
}
}
}
#line 1 "test/judge.yosupo.jp/Persistent_UnionFind.0.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/persistent_unionfind"
#line 2 "data_structure/persistent_unionfind.hpp"
#line 2 "template.hpp"
#include <bits/stdc++.h>
using namespace std;
#define all(a) begin(a), end(a)
#define rall(a) rbegin(a), rend(a)
#define uniq(a) (a).erase(unique(all(a)), (a).end())
#define t0 first
#define t1 second
using ll = long long;
using ull = unsigned long long;
using pll = pair<ll, ll>;
using vll = vector<ll>;
constexpr double pi = 3.14159265358979323846;
constexpr ll dy[9] = {0, 1, 0, -1, 1, 1, -1, -1, 0};
constexpr ll dx[9] = {1, 0, -1, 0, 1, -1, -1, 1, 0};
constexpr ll sign(ll a) { return (a > 0) - (a < 0); }
constexpr ll fdiv(ll a, ll b) { return a / b - ((a ^ b) < 0 && a % b); }
constexpr ll cdiv(ll a, ll b) { return -fdiv(-a, b); }
constexpr ll pw(ll n) { return 1ll << n; }
constexpr ll flg(ll n) { return 63 - __builtin_clzll(n); }
constexpr ll clg(ll n) { return n == 1 ? 0 : flg(n - 1) + 1; }
constexpr ll safemod(ll x, ll mod) { return (x % mod + mod) % mod; }
template <typename T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>;
template <typename T> constexpr T sq(const T &a) { return a * a; }
template <typename T, typename U> constexpr bool chmax(T &a, const U &b) { return a < b ? a = b, true : false; }
template <typename T, typename U> constexpr bool chmin(T &a, const U &b) { return a > b ? a = b, true : false; }
template <typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &a) {
os << "(" << a.first << ", " << a.second << ")";
return os;
}
template <typename T, typename U, typename V> ostream &operator<<(ostream &os, const tuple<T, U, V> &a) {
os << "(" << get<0>(a) << ", " << get<1>(a) << ", " << get<2>(a) << ")";
return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &a) {
os << "(";
for (auto itr = a.begin(); itr != a.end(); ++itr) os << *itr << (next(itr) != a.end() ? ", " : "");
os << ")";
return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &a) {
os << "(";
for (auto itr = a.begin(); itr != a.end(); ++itr) os << *itr << (next(itr) != a.end() ? ", " : "");
os << ")";
return os;
}
template <typename T> ostream &operator<<(ostream &os, const multiset<T> &a) {
os << "(";
for (auto itr = a.begin(); itr != a.end(); ++itr) os << *itr << (next(itr) != a.end() ? ", " : "");
os << ")";
return os;
}
template <typename T, typename U> ostream &operator<<(ostream &os, const map<T, U> &a) {
os << "(";
for (auto itr = a.begin(); itr != a.end(); ++itr) os << *itr << (next(itr) != a.end() ? ", " : "");
os << ")";
return os;
}
#ifdef ONLINE_JUDGE
#define dump(...) (void(0))
#else
void debug() { cerr << endl; }
template <typename Head, typename... Tail> void debug(Head &&head, Tail &&... tail) {
cerr << head;
if (sizeof...(Tail)) cerr << ", ";
debug(tail...);
}
#define dump(...) cerr << __LINE__ << ": " << #__VA_ARGS__ << " = ", debug(__VA_ARGS__)
#endif
struct rep {
struct itr {
ll v;
itr(ll v) : v(v) {}
void operator++() { ++v; }
ll operator*() const { return v; }
bool operator!=(itr i) const { return v < *i; }
};
ll l, r;
rep(ll l, ll r) : l(l), r(r) {}
rep(ll r) : rep(0, r) {}
itr begin() const { return l; };
itr end() const { return r; };
};
struct per {
struct itr {
ll v;
itr(ll v) : v(v) {}
void operator++() { --v; }
ll operator*() const { return v; }
bool operator!=(itr i) const { return v > *i; }
};
ll l, r;
per(ll l, ll r) : l(l), r(r) {}
per(ll r) : per(0, r) {}
itr begin() const { return r - 1; };
itr end() const { return l - 1; };
};
struct io_setup {
static constexpr int PREC = 20;
io_setup() {
cout << fixed << setprecision(PREC);
cerr << fixed << setprecision(PREC);
};
} iOS;
#line 2 "data_structure/persistent_array.hpp"
#line 4 "data_structure/persistent_array.hpp"
template <typename V, ll SHIFT> struct persistent_array {
using ptr = shared_ptr<persistent_array>;
static constexpr ll BASE = pw(SHIFT);
static constexpr ll MASK = BASE - 1;
V val;
array<ptr, BASE> ch;
persistent_array(ll n = 1, V val = V()) : val(val) {
for (ll i : rep(BASE)) {
ll m = (n >> SHIFT) + ((n & MASK) > i);
if (m > 1 || m > 0 && i > 0) ch[i] = ptr(new persistent_array(m, val));
}
}
persistent_array(V val, const array<ptr, BASE> &ch) : val(val), ch(ch) {}
persistent_array(V val, const array<ptr, BASE> &ch, ll i, ptr chp) : val(val), ch(ch) { this->ch[i] = chp; }
V get(ll i) const { return i > 0 ? ch[i & MASK]->get(i >> SHIFT) : val; }
ptr setp(ll i, V val) const {
return ptr(i > 0 ? new persistent_array(this->val, ch, i & MASK, ch[i & MASK]->setp(i >> SHIFT, val))
: new persistent_array(val, ch));
}
persistent_array set(ll i, V val) const { return *setp(i, val); }
};
#line 5 "data_structure/persistent_unionfind.hpp"
template <ll SHIFT> struct persistent_unionfind {
ll n;
persistent_array<ll, SHIFT> ps;
persistent_unionfind(ll n) : n(n), ps(n, -1) {}
persistent_unionfind(ll n, persistent_array<ll, SHIFT> ps) : n(n), ps(ps) {}
pair<ll, ll> find_with_size(ll i) const {
ll psi = ps.get(i);
return psi < 0 ? make_pair(i, -psi) : find_with_size(psi);
}
ll find(ll i) const { return find_with_size(i).first; }
ll size(ll i) const { return find_with_size(i).second; }
bool same(ll i, ll j) const { return find(i) == find(j); }
persistent_unionfind unite(ll i, ll j) const {
auto [ip, si] = find_with_size(i);
auto [jp, sj] = find_with_size(j);
if (ip == jp) return *this;
if (si < sj) swap(ip, jp);
return persistent_unionfind(n, ps.set(ip, -si - sj).set(jp, ip));
}
vector<vector<ll>> groups() const {
vector<vector<ll>> ret(n);
for (ll i : rep(n)) ret[find(i)].push_back(i);
ret.erase(remove_if(all(ret), [](const vector<ll> &v) { return v.empty(); }), ret.end());
return ret;
}
};
#line 3 "test/judge.yosupo.jp/Persistent_UnionFind.0.test.cpp"
int main() {
ll n, q;
cin >> n >> q;
vector<persistent_unionfind<4>> his;
his.emplace_back(n);
for (ll i : rep(q)) {
ll c, k, u, v;
cin >> c >> k >> u >> v, ++k;
if (c == 0) { his.emplace_back(his[k].unite(u, v)); }
if (c == 1) {
cout << his[k].same(u, v) << '\n';
his.emplace_back(1);
}
}
}