This documentation is automatically generated by online-judge-tools/verification-helper
#define PROBLEM "https://judge.yosupo.jp/problem/unionfind"
#include <bits/stdc++.h>
using namespace std;
#include "union_find.hpp"
int main() {
int N, Q;
cin >> N >> Q;
UnionFind uf(N);
for (int q = 0; q < Q; ++q) {
int type, x, y;
cin >> type >> x >> y;
if (type == 0) uf.merge(x, y);
else {
if (uf.same(x, y)) cout << 1 << endl;
else cout << 0 << endl;
}
//cout << uf << endl;
}
}#line 1 "tests/LibraryChecker_Unionfind.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/unionfind"
#include <bits/stdc++.h>
using namespace std;
#line 3 "DataStructure/union_find.hpp"
using namespace std;
// Union-Find
struct UnionFind {
// core member
vector<int> par, nex;
// constructor
UnionFind() { }
UnionFind(int N) : par(N, -1), nex(N) {
init(N);
}
void init(int N) {
par.assign(N, -1);
nex.resize(N);
for (int i = 0; i < N; ++i) nex[i] = i;
}
// core methods
int root(int x) {
if (par[x] < 0) return x;
else return par[x] = root(par[x]);
}
bool same(int x, int y) {
return root(x) == root(y);
}
bool merge(int x, int y) {
x = root(x), y = root(y);
if (x == y) return false;
if (par[x] > par[y]) swap(x, y); // merge technique
par[x] += par[y];
par[y] = x;
swap(nex[x], nex[y]);
return true;
}
int size(int x) {
return -par[root(x)];
}
// get group
vector<int> group(int x) {
vector<int> res({x});
while (nex[res.back()] != x) res.push_back(nex[res.back()]);
return res;
}
vector<vector<int>> groups() {
vector<vector<int>> member(par.size());
for (int v = 0; v < (int)par.size(); ++v) {
member[root(v)].push_back(v);
}
vector<vector<int>> res;
for (int v = 0; v < (int)par.size(); ++v) {
if (!member[v].empty()) res.push_back(member[v]);
}
return res;
}
// debug
friend ostream& operator << (ostream &s, UnionFind uf) {
const vector<vector<int>> &gs = uf.groups();
for (const vector<int> &g : gs) {
s << "group: ";
for (int v : g) s << v << " ";
s << endl;
}
return s;
}
};
#line 7 "tests/LibraryChecker_Unionfind.test.cpp"
int main() {
int N, Q;
cin >> N >> Q;
UnionFind uf(N);
for (int q = 0; q < Q; ++q) {
int type, x, y;
cin >> type >> x >> y;
if (type == 0) uf.merge(x, y);
else {
if (uf.same(x, y)) cout << 1 << endl;
else cout << 0 << endl;
}
//cout << uf << endl;
}
}