This documentation is automatically generated by online-judge-tools/verification-helper
View the Project on GitHub knshnb/competitive_library
#define PROBLEM "https://judge.yosupo.jp/problem/unionfind" #include <bits/stdc++.h> // clang-format off using Int = long long; #define REP_(i, a_, b_, a, b, ...) for (Int i = (a), lim##i = (b); i < lim##i; i++) #define REP(i, ...) REP_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__) struct SetupIO { SetupIO() { std::cin.tie(nullptr), std::ios::sync_with_stdio(false), std::cout << std::fixed << std::setprecision(13); } } setup_io; #ifndef _MY_DEBUG #define dump(...) #endif // clang-format on /** * author: knshnb * created: Sun Mar 29 05:04:23 JST 2020 **/ #define CALL_FROM_TEST #include "../../src/DataStructure/UnionFind.hpp" #undef CALL_FROM_TEST signed main() { Int n, Q; std::cin >> n >> Q; UnionFind uf(n); REP(q, Q) { Int t, u, v; std::cin >> t >> u >> v; if (t == 0) { uf.unite(u, v); } else { std::cout << uf.is_same(u, v) << "\n"; } } }
#line 1 "test/yosupo/unionfind.test.cpp" #define PROBLEM "https://judge.yosupo.jp/problem/unionfind" #include <bits/stdc++.h> // clang-format off using Int = long long; #define REP_(i, a_, b_, a, b, ...) for (Int i = (a), lim##i = (b); i < lim##i; i++) #define REP(i, ...) REP_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__) struct SetupIO { SetupIO() { std::cin.tie(nullptr), std::ios::sync_with_stdio(false), std::cout << std::fixed << std::setprecision(13); } } setup_io; #ifndef _MY_DEBUG #define dump(...) #endif // clang-format on /** * author: knshnb * created: Sun Mar 29 05:04:23 JST 2020 **/ #define CALL_FROM_TEST #line 1 "src/DataStructure/UnionFind.hpp" struct UnionFind { int cnt; // 集合の数 std::vector<int> number; // 0以上のとき親のindex, 負のときは集合サイズ UnionFind(int n) : cnt(n), number(n, -1) {} int root(int x) { return number[x] < 0 ? x : number[x] = root(number[x]); } void unite(int x, int y) { x = root(x), y = root(y); if (x == y) return; if (number[y] < number[x]) std::swap(x, y); // yをxにマージ number[x] += number[y]; number[y] = x; cnt--; } bool is_same(int x, int y) { return root(x) == root(y); } int size(int x) { return -number[root(x)]; } }; #line 19 "test/yosupo/unionfind.test.cpp" #undef CALL_FROM_TEST signed main() { Int n, Q; std::cin >> n >> Q; UnionFind uf(n); REP(q, Q) { Int t, u, v; std::cin >> t >> u >> v; if (t == 0) { uf.unite(u, v); } else { std::cout << uf.is_same(u, v) << "\n"; } } }