結果

問題 No.2290 UnUnion Find
ユーザー ynm3nynm3n
提出日時 2023-05-06 00:15:55
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 126 ms / 2,000 ms
コード長 1,828 bytes
コンパイル時間 1,125 ms
コンパイル使用メモリ 91,284 KB
実行使用メモリ 13,352 KB
最終ジャッジ日時 2024-05-02 19:18:32
合計ジャッジ時間 10,963 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 44 ms
5,376 KB
testcase_03 AC 86 ms
8,236 KB
testcase_04 AC 89 ms
8,228 KB
testcase_05 AC 86 ms
8,232 KB
testcase_06 AC 88 ms
8,360 KB
testcase_07 AC 87 ms
8,232 KB
testcase_08 AC 87 ms
8,360 KB
testcase_09 AC 88 ms
8,228 KB
testcase_10 AC 92 ms
8,232 KB
testcase_11 AC 100 ms
8,360 KB
testcase_12 AC 88 ms
8,232 KB
testcase_13 AC 87 ms
8,236 KB
testcase_14 AC 91 ms
8,236 KB
testcase_15 AC 97 ms
8,236 KB
testcase_16 AC 95 ms
8,228 KB
testcase_17 AC 92 ms
8,360 KB
testcase_18 AC 98 ms
8,232 KB
testcase_19 AC 113 ms
8,532 KB
testcase_20 AC 125 ms
13,344 KB
testcase_21 AC 49 ms
5,376 KB
testcase_22 AC 70 ms
5,624 KB
testcase_23 AC 70 ms
5,752 KB
testcase_24 AC 114 ms
9,808 KB
testcase_25 AC 61 ms
5,376 KB
testcase_26 AC 121 ms
13,248 KB
testcase_27 AC 116 ms
10,108 KB
testcase_28 AC 87 ms
6,628 KB
testcase_29 AC 115 ms
9,384 KB
testcase_30 AC 54 ms
5,376 KB
testcase_31 AC 107 ms
8,820 KB
testcase_32 AC 65 ms
5,376 KB
testcase_33 AC 87 ms
6,748 KB
testcase_34 AC 126 ms
13,352 KB
testcase_35 AC 123 ms
10,680 KB
testcase_36 AC 69 ms
5,596 KB
testcase_37 AC 81 ms
6,324 KB
testcase_38 AC 66 ms
5,376 KB
testcase_39 AC 70 ms
5,760 KB
testcase_40 AC 111 ms
10,128 KB
testcase_41 AC 61 ms
5,376 KB
testcase_42 AC 104 ms
8,392 KB
testcase_43 AC 98 ms
8,240 KB
testcase_44 AC 95 ms
8,356 KB
testcase_45 AC 93 ms
8,228 KB
testcase_46 AC 97 ms
8,232 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std;

struct UnionFindTree {
    vector<int> parent;
    int cnt;
    unordered_set<int> roots;

    UnionFindTree(int n) {
        parent.assign(n, -1);
        cnt = n;
        for (int i = 0; i < n; i++) {
            roots.insert(i);
        }
    }

    int findRoot(int a) {
        if (parent[a] < 0) {
            return a;
        }
        int r = findRoot(parent[a]);
        if (roots.count(a) && a != r) {
            roots.erase(a);
        }
        parent[a] = r;
        return r;
    }

    bool unite(int a, int b) {
        int x = findRoot(a), y = findRoot(b);
        if (x == y) {
            return false;
        }
        if (size(x) < size(y)) {
            swap(x, y);
        }
        roots.erase(y);
        parent[x] += parent[y];
        parent[y] = x;
        cnt--;
        return true;
    }

    bool sameRoot(int a, int b) {
        return findRoot(a) == findRoot(b);
    }

    int size(int a) {
        return -parent[findRoot(a)];
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, q;
    cin >> n >> q;

    UnionFindTree uf(n);
    int typ, u, v;
    for (int i = 0; i < q; i++) {
        cin >> typ;
        switch (typ) {
        case 1:
            cin >> u >> v;
            u--, v--;
            uf.unite(u, v);
            break;
        case 2:
            cin >> v;
            v--;
            int r = uf.findRoot(v);
            int ans = -1;
            for (auto it = uf.roots.begin(); it != uf.roots.end(); it++) {
                int v2 = *it;
                if (r != v2) {
                    ans = v2 + 1;
                    break;
                }
            }
            cout << ans << "\n";
            break;
        }
    }

    return 0;
}
0