competitive_library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub knshnb/competitive_library

:heavy_check_mark: test/yosupo/unionfind_quick_find.test.cpp

Depends on

Code

#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 dump
#define dump(...)
#endif  // clang-format on

/**
 *    author:  knshnb
 *    created: Mon Apr 27 23:29:45 JST 2020
 **/

#define CALL_FROM_TEST
#include "../../src/DataStructure/QuickFind.hpp"
#undef CALL_FROM_TEST

signed main() {
    Int n, Q;
    std::cin >> n >> Q;
    QuickFind qf(n);
    REP(q, Q) {
        Int t, u, v;
        std::cin >> t >> u >> v;
        if (t == 0) {
            qf.unite(u, v);
        } else {
            std::cout << qf.is_same(u, v) << "\n";
        }
    }
}
#line 1 "test/yosupo/unionfind_quick_find.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 dump
#define dump(...)
#endif  // clang-format on

/**
 *    author:  knshnb
 *    created: Mon Apr 27 23:29:45 JST 2020
 **/

#define CALL_FROM_TEST
#line 1 "src/DataStructure/QuickFind.hpp"
/// @docs src/DataStructure/QuickFind.md
struct QuickFind {
    std::vector<int> belong_to;
    std::vector<std::vector<int>> groups;
    QuickFind(int n) : belong_to(n), groups(n, std::vector<int>(1)) {
        std::iota(belong_to.begin(), belong_to.end(), 0);
        for (int i = 0; i < n; i++) groups[i][0] = i;
    }
    void unite(int x, int y) {
        x = belong_to[x], y = belong_to[y];
        if (x == y) return;
        if (groups[x].size() < groups[y].size()) std::swap(x, y);
        // yをxにマージ
        for (int v : groups[y]) belong_to[v] = x;
        groups[x].insert(groups[x].end(), groups[y].begin(), groups[y].end());
        groups[y].clear();
    }
    bool is_same(int x, int y) { return belong_to[x] == belong_to[y]; }
};
#line 19 "test/yosupo/unionfind_quick_find.test.cpp"
#undef CALL_FROM_TEST

signed main() {
    Int n, Q;
    std::cin >> n >> Q;
    QuickFind qf(n);
    REP(q, Q) {
        Int t, u, v;
        std::cin >> t >> u >> v;
        if (t == 0) {
            qf.unite(u, v);
        } else {
            std::cout << qf.is_same(u, v) << "\n";
        }
    }
}
Back to top page