結果

問題 No.2290 UnUnion Find
ユーザー 👑 tatyamtatyam
提出日時 2023-05-05 21:47:54
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 445 ms / 2,000 ms
コード長 1,029 bytes
コンパイル時間 3,743 ms
コンパイル使用メモリ 263,216 KB
実行使用メモリ 26,112 KB
最終ジャッジ日時 2024-05-02 15:51:44
合計ジャッジ時間 18,017 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 31 ms
5,376 KB
testcase_03 AC 159 ms
15,360 KB
testcase_04 AC 160 ms
15,144 KB
testcase_05 AC 167 ms
15,232 KB
testcase_06 AC 164 ms
15,232 KB
testcase_07 AC 164 ms
15,232 KB
testcase_08 AC 165 ms
15,232 KB
testcase_09 AC 168 ms
15,232 KB
testcase_10 AC 169 ms
15,232 KB
testcase_11 AC 167 ms
15,144 KB
testcase_12 AC 165 ms
15,104 KB
testcase_13 AC 163 ms
15,232 KB
testcase_14 AC 160 ms
15,144 KB
testcase_15 AC 162 ms
15,232 KB
testcase_16 AC 162 ms
15,232 KB
testcase_17 AC 166 ms
15,140 KB
testcase_18 AC 162 ms
15,232 KB
testcase_19 AC 277 ms
16,896 KB
testcase_20 AC 445 ms
26,112 KB
testcase_21 AC 47 ms
5,376 KB
testcase_22 AC 119 ms
9,216 KB
testcase_23 AC 126 ms
9,472 KB
testcase_24 AC 346 ms
20,480 KB
testcase_25 AC 93 ms
7,808 KB
testcase_26 AC 400 ms
23,424 KB
testcase_27 AC 371 ms
21,504 KB
testcase_28 AC 205 ms
13,184 KB
testcase_29 AC 327 ms
19,328 KB
testcase_30 AC 58 ms
5,504 KB
testcase_31 AC 295 ms
17,792 KB
testcase_32 AC 98 ms
8,004 KB
testcase_33 AC 199 ms
13,184 KB
testcase_34 AC 438 ms
25,984 KB
testcase_35 AC 403 ms
23,424 KB
testcase_36 AC 107 ms
8,448 KB
testcase_37 AC 172 ms
11,776 KB
testcase_38 AC 107 ms
8,320 KB
testcase_39 AC 131 ms
9,728 KB
testcase_40 AC 379 ms
22,144 KB
testcase_41 AC 89 ms
7,424 KB
testcase_42 AC 271 ms
16,000 KB
testcase_43 AC 253 ms
15,488 KB
testcase_44 AC 168 ms
15,272 KB
testcase_45 AC 177 ms
15,232 KB
testcase_46 AC 198 ms
15,232 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int main() {
    cin.tie(0)->sync_with_stdio(0);

    int N, Q;
    cin >> N >> Q;
    atcoder::dsu uf(N);
    map<int, vector<int>> groups;
    for(int i = 0; i < N; i++) groups[i] = {i};

    while(Q--) {
        int t;
        cin >> t;
        if(t == 1) {
            int u, v;
            cin >> u >> v;
            u = uf.leader(u - 1);
            v = uf.leader(v - 1);
            if(u == v) continue;
            if(uf.merge(u, v) == v) swap(u, v);
            vector<int>& U = groups[u], & V = groups[v];
            U.insert(U.end(), V.begin(), V.end());
            groups.erase(v);
        }
        else {
            int u;
            cin >> u;
            u = uf.leader(u - 1);
            if(uf.size(u) == N) {
                cout << "-1\n";
                continue;
            }
            auto p = groups.find(u);
            if(++p == groups.end()) p = groups.begin();
            cout << p->second[0] + 1 << '\n';
        }
    }
}
0