結果

問題 No.2290 UnUnion Find
ユーザー t98slidert98slider
提出日時 2023-02-04 13:25:28
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 397 ms / 2,000 ms
コード長 1,527 bytes
コンパイル時間 1,682 ms
コンパイル使用メモリ 176,420 KB
実行使用メモリ 13,636 KB
最終ジャッジ日時 2023-10-24 06:42:16
合計ジャッジ時間 21,782 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 328 ms
4,348 KB
testcase_03 AC 315 ms
8,372 KB
testcase_04 AC 318 ms
8,372 KB
testcase_05 AC 314 ms
8,372 KB
testcase_06 AC 316 ms
8,372 KB
testcase_07 AC 315 ms
8,372 KB
testcase_08 AC 315 ms
8,372 KB
testcase_09 AC 314 ms
8,372 KB
testcase_10 AC 314 ms
8,372 KB
testcase_11 AC 314 ms
8,372 KB
testcase_12 AC 317 ms
8,372 KB
testcase_13 AC 312 ms
8,372 KB
testcase_14 AC 315 ms
8,372 KB
testcase_15 AC 323 ms
8,372 KB
testcase_16 AC 315 ms
8,372 KB
testcase_17 AC 317 ms
8,372 KB
testcase_18 AC 321 ms
8,372 KB
testcase_19 AC 344 ms
9,148 KB
testcase_20 AC 394 ms
13,636 KB
testcase_21 AC 334 ms
4,348 KB
testcase_22 AC 332 ms
6,032 KB
testcase_23 AC 329 ms
6,032 KB
testcase_24 AC 364 ms
10,732 KB
testcase_25 AC 334 ms
5,372 KB
testcase_26 AC 381 ms
12,316 KB
testcase_27 AC 368 ms
11,260 KB
testcase_28 AC 336 ms
7,564 KB
testcase_29 AC 356 ms
10,204 KB
testcase_30 AC 334 ms
4,512 KB
testcase_31 AC 352 ms
9,676 KB
testcase_32 AC 333 ms
5,512 KB
testcase_33 AC 335 ms
7,564 KB
testcase_34 AC 397 ms
13,372 KB
testcase_35 AC 389 ms
12,052 KB
testcase_36 AC 335 ms
5,676 KB
testcase_37 AC 335 ms
6,968 KB
testcase_38 AC 333 ms
5,636 KB
testcase_39 AC 330 ms
6,204 KB
testcase_40 AC 375 ms
11,524 KB
testcase_41 AC 337 ms
5,256 KB
testcase_42 AC 349 ms
8,884 KB
testcase_43 AC 344 ms
8,620 KB
testcase_44 AC 318 ms
8,372 KB
testcase_45 AC 331 ms
8,376 KB
testcase_46 AC 334 ms
8,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

struct dsu {
    public:
    dsu() : _n(0) {}
    dsu(int n) : _n(n), parent_or_size(n, -1) {
        for(int i = 0; i < _n; i++){
            leader_set.insert(i);
        }
    }

    int merge(int a, int b) {
        assert(0 <= a && a < _n);
        assert(0 <= b && b < _n);
        int x = leader(a), y = leader(b);
        if (x == y) return x;
        if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y);
        leader_set.erase(y);
        parent_or_size[x] += parent_or_size[y];
        parent_or_size[y] = x;
        return x;
    }

    bool same(int a, int b) {
        assert(0 <= a && a < _n);
        assert(0 <= b && b < _n);
        return leader(a) == leader(b);
    }

    int leader(int a) {
        assert(0 <= a && a < _n);
        if (parent_or_size[a] < 0) return a;
        return parent_or_size[a] = leader(parent_or_size[a]);
    }

    int get(int a) {
        assert(0 <= a && a < _n);
        for(auto &v : leader_set){
            if(!same(v, a))return v + 1;
        }
        return -1;
    }

    private:
    int _n;
    // root node: -1 * component size
    // otherwise: parent
    std::set<int> leader_set;
    std::vector<int> parent_or_size;
};

int main(){
    int N, Q, cmd, u, v;
    cin >> N >> Q;
    dsu uf(N);
    while(Q--){
        cin >> cmd;
        if(cmd == 1){
            cin >> u >> v;
            uf.merge(--u, --v);
        }else{
            cin >> v;
            cout << uf.get(--v) << '\n';
        }
    }
}
0