結果

問題 No.2290 UnUnion Find
ユーザー rniyarniya
提出日時 2023-05-05 21:25:16
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 219 ms / 2,000 ms
コード長 3,072 bytes
コンパイル時間 2,069 ms
コンパイル使用メモリ 213,144 KB
実行使用メモリ 13,440 KB
最終ジャッジ日時 2024-05-02 15:06:43
合計ジャッジ時間 11,633 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 32 ms
5,376 KB
testcase_03 AC 106 ms
8,320 KB
testcase_04 AC 108 ms
8,192 KB
testcase_05 AC 105 ms
8,192 KB
testcase_06 AC 104 ms
8,320 KB
testcase_07 AC 108 ms
8,192 KB
testcase_08 AC 105 ms
8,320 KB
testcase_09 AC 107 ms
8,192 KB
testcase_10 AC 108 ms
8,192 KB
testcase_11 AC 107 ms
8,320 KB
testcase_12 AC 107 ms
8,320 KB
testcase_13 AC 108 ms
8,320 KB
testcase_14 AC 106 ms
8,192 KB
testcase_15 AC 102 ms
8,192 KB
testcase_16 AC 105 ms
8,192 KB
testcase_17 AC 110 ms
8,192 KB
testcase_18 AC 108 ms
8,320 KB
testcase_19 AC 138 ms
9,088 KB
testcase_20 AC 197 ms
13,312 KB
testcase_21 AC 41 ms
5,376 KB
testcase_22 AC 79 ms
5,888 KB
testcase_23 AC 76 ms
5,888 KB
testcase_24 AC 170 ms
10,752 KB
testcase_25 AC 69 ms
5,376 KB
testcase_26 AC 189 ms
12,160 KB
testcase_27 AC 175 ms
11,264 KB
testcase_28 AC 113 ms
7,352 KB
testcase_29 AC 169 ms
10,240 KB
testcase_30 AC 49 ms
5,376 KB
testcase_31 AC 163 ms
9,472 KB
testcase_32 AC 73 ms
5,376 KB
testcase_33 AC 115 ms
7,296 KB
testcase_34 AC 219 ms
13,440 KB
testcase_35 AC 185 ms
11,904 KB
testcase_36 AC 72 ms
5,504 KB
testcase_37 AC 96 ms
6,784 KB
testcase_38 AC 71 ms
5,500 KB
testcase_39 AC 84 ms
5,888 KB
testcase_40 AC 177 ms
11,520 KB
testcase_41 AC 64 ms
5,376 KB
testcase_42 AC 139 ms
8,704 KB
testcase_43 AC 134 ms
8,448 KB
testcase_44 AC 108 ms
8,320 KB
testcase_45 AC 108 ms
8,320 KB
testcase_46 AC 107 ms
8,320 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#ifdef LOCAL
#include <debug.hpp>
#else
#define debug(...) void(0)
#endif

struct UnionFind {
    UnionFind(int n) : n(n), num(n), data(n, -1) {}

    int find(int x) {
        assert(0 <= x && x < n);
        return data[x] < 0 ? x : data[x] = find(data[x]);
    }

    bool merge(int x, int y) {
        assert(0 <= x && x < n);
        assert(0 <= y && y < n);
        if ((x = find(x)) == (y = find(y))) return false;
        if (-data[x] < -data[y]) std::swap(x, y);
        data[x] += data[y];
        data[y] = x;
        num--;
        return true;
    }

    bool same(int x, int y) {
        assert(0 <= x && x < n);
        assert(0 <= y && y < n);
        return find(x) == find(y);
    }

    int size(int x) {
        assert(0 <= x && x < n);
        return -data[find(x)];
    }

    int count() const { return num; }

    std::vector<std::vector<int>> groups() {
        std::vector<std::vector<int>> res(n);
        for (int i = 0; i < n; i++) res[find(i)].emplace_back(i);
        res.erase(std::remove_if(res.begin(), res.end(), [&](const std::vector<int>& v) { return v.empty(); }));
        return res;
    }

    int operator[](int x) { return find(x); }

private:
    int n, num;
    // root node : -1 * component size
    // otherwise : parent
    std::vector<int> data;
};

using namespace std;

typedef long long ll;
#define all(x) begin(x), end(x)
constexpr int INF = (1 << 30) - 1;
constexpr long long IINF = (1LL << 60) - 1;
constexpr int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

template <class T> istream& operator>>(istream& is, vector<T>& v) {
    for (auto& x : v) is >> x;
    return is;
}

template <class T> ostream& operator<<(ostream& os, const vector<T>& v) {
    auto sep = "";
    for (const auto& x : v) os << exchange(sep, " ") << x;
    return os;
}

template <class T, class U = T> bool chmin(T& x, U&& y) { return y < x and (x = forward<U>(y), true); }

template <class T, class U = T> bool chmax(T& x, U&& y) { return x < y and (x = forward<U>(y), true); }

template <class T> void mkuni(vector<T>& v) {
    sort(begin(v), end(v));
    v.erase(unique(begin(v), end(v)), end(v));
}

template <class T> int lwb(const vector<T>& v, const T& x) { return lower_bound(begin(v), end(v), x) - begin(v); }

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int N, Q;
    cin >> N >> Q;
    set<int> s;
    for (int i = 0; i < N; i++) s.emplace(i);
    UnionFind UF(N);
    for (; Q--;) {
        int t;
        cin >> t;
        if (t == 1) {
            int u, v;
            cin >> u >> v;
            u = UF[u - 1], v = UF[v - 1];
            if (u == v) continue;
            s.erase(u);
            s.erase(v);
            UF.merge(u, v);
            s.emplace(UF[u]);
        } else {
            int v;
            cin >> v;
            v = UF[v - 1];
            if (UF.count() == 1) {
                cout << -1 << '\n';
                continue;
            }
            cout << (*s.begin() != v ? *s.begin() : *s.rbegin()) + 1 << '\n';
        }
    }
    return 0;
}
0