This documentation is automatically generated by online-judge-tools/verification-helper
View the Project on GitHub kmyk/competitive-programming-library
#define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DSL_1_A" #include "../data_structure/union_find_tree.hpp" #include <cstdio> int main() { int n, q; scanf("%d%d", &n, &q); union_find_tree uft(n); while (q --) { int com, x, y; scanf("%d%d%d", &com, &x, &y); if (com == 0) { uft.unite_trees(x, y); } else if (com == 1) { bool answer = uft.is_same(x, y); printf("%d\n", (int)answer); } } return 0; }
#line 1 "data_structure/union_find_tree.aoj.test.cpp" #define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DSL_1_A" #line 2 "data_structure/union_find_tree.hpp" #include <vector> /** * @brief Union-Find Tree * @docs data_structure/union_find_tree.md * @note union-by-size + path-compression */ struct union_find_tree { std::vector<int> data; union_find_tree() = default; explicit union_find_tree(std::size_t n) : data(n, -1) {} bool is_root(int i) { return data[i] < 0; } int find_root(int i) { return is_root(i) ? i : (data[i] = find_root(data[i])); } int tree_size(int i) { return - data[find_root(i)]; } int unite_trees(int i, int j) { i = find_root(i); j = find_root(j); if (i != j) { if (tree_size(i) < tree_size(j)) std::swap(i, j); data[i] += data[j]; data[j] = i; } return i; } bool is_same(int i, int j) { return find_root(i) == find_root(j); } }; #line 3 "data_structure/union_find_tree.aoj.test.cpp" #include <cstdio> int main() { int n, q; scanf("%d%d", &n, &q); union_find_tree uft(n); while (q --) { int com, x, y; scanf("%d%d%d", &com, &x, &y); if (com == 0) { uft.unite_trees(x, y); } else if (com == 1) { bool answer = uft.is_same(x, y); printf("%d\n", (int)answer); } } return 0; }