This documentation is automatically generated by online-judge-tools/verification-helper
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DSL_1_A"
#include <iostream>
#include "../../../library/data-structure/dsu.hpp"
using namespace std;
using namespace felix;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, q;
cin >> n >> q;
dsu<true> d(n);
while(q--) {
int type, x, y;
cin >> type >> x >> y;
if(type == 0) {
d.merge(x, y);
} else {
cout << d.same(x, y) << "\n";
}
}
return 0;
}
#line 1 "test/data-structure/dsu/aoj-dsl-Disjoint-Set-Union-Find-Tree.test.cpp"
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DSL_1_A"
#include <iostream>
#line 2 "library/data-structure/dsu.hpp"
#include <vector>
#include <cassert>
#include <algorithm>
namespace felix {
template<bool UNION_BY_SIZE = false>
struct dsu {
public:
dsu() : dsu(0) {}
explicit dsu(int _n) : n(_n), sz(n, -1) {}
int leader(int u) {
assert(0 <= u && u < n);
return (sz[u] < 0 ? u : (sz[u] = leader(sz[u])));
}
bool merge(int a, int b) {
a = leader(a), b = leader(b);
if(a == b) {
return false;
}
if constexpr(UNION_BY_SIZE) {
if(-sz[a] < -sz[b]) {
std::swap(a, b);
}
}
sz[a] += sz[b];
sz[b] = a;
return true;
}
int size(int u) { return -sz[leader(u)]; }
bool same(int a, int b) { return leader(a) == leader(b); }
std::vector<std::vector<int>> groups() {
std::vector<std::vector<int>> result(n);
for(int i = 0; i < n; i++) {
result[leader(i)].push_back(i);
}
result.erase(std::remove_if(result.begin(), result.end(), [](const std::vector<int>& v) {
return v.empty();
}), result.end());
return result;
}
private:
int n;
std::vector<int> sz;
};
} // namespace felix
#line 5 "test/data-structure/dsu/aoj-dsl-Disjoint-Set-Union-Find-Tree.test.cpp"
using namespace std;
using namespace felix;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, q;
cin >> n >> q;
dsu<true> d(n);
while(q--) {
int type, x, y;
cin >> type >> x >> y;
if(type == 0) {
d.merge(x, y);
} else {
cout << d.same(x, y) << "\n";
}
}
return 0;
}