結果

問題 No.2290 UnUnion Find
ユーザー 👑 tatyamtatyam
提出日時 2023-05-05 21:47:54
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 421 ms / 2,000 ms
コード長 1,029 bytes
コンパイル時間 4,250 ms
コンパイル使用メモリ 261,084 KB
実行使用メモリ 25,724 KB
最終ジャッジ日時 2023-08-15 03:38:17
合計ジャッジ時間 18,899 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 31 ms
4,388 KB
testcase_03 AC 153 ms
14,964 KB
testcase_04 AC 152 ms
15,104 KB
testcase_05 AC 154 ms
15,036 KB
testcase_06 AC 158 ms
15,080 KB
testcase_07 AC 153 ms
15,080 KB
testcase_08 AC 154 ms
14,980 KB
testcase_09 AC 156 ms
14,972 KB
testcase_10 AC 151 ms
14,976 KB
testcase_11 AC 158 ms
15,104 KB
testcase_12 AC 157 ms
15,020 KB
testcase_13 AC 160 ms
15,032 KB
testcase_14 AC 164 ms
14,992 KB
testcase_15 AC 159 ms
15,056 KB
testcase_16 AC 150 ms
15,028 KB
testcase_17 AC 152 ms
14,968 KB
testcase_18 AC 154 ms
14,992 KB
testcase_19 AC 271 ms
16,876 KB
testcase_20 AC 421 ms
25,724 KB
testcase_21 AC 45 ms
4,620 KB
testcase_22 AC 117 ms
9,184 KB
testcase_23 AC 117 ms
9,244 KB
testcase_24 AC 319 ms
20,244 KB
testcase_25 AC 89 ms
7,588 KB
testcase_26 AC 362 ms
23,280 KB
testcase_27 AC 344 ms
21,328 KB
testcase_28 AC 187 ms
12,908 KB
testcase_29 AC 306 ms
19,224 KB
testcase_30 AC 56 ms
5,424 KB
testcase_31 AC 290 ms
17,736 KB
testcase_32 AC 95 ms
7,860 KB
testcase_33 AC 183 ms
12,980 KB
testcase_34 AC 402 ms
25,676 KB
testcase_35 AC 356 ms
23,232 KB
testcase_36 AC 100 ms
8,600 KB
testcase_37 AC 158 ms
11,680 KB
testcase_38 AC 98 ms
8,260 KB
testcase_39 AC 123 ms
9,736 KB
testcase_40 AC 337 ms
21,916 KB
testcase_41 AC 84 ms
7,272 KB
testcase_42 AC 241 ms
15,808 KB
testcase_43 AC 226 ms
15,404 KB
testcase_44 AC 154 ms
15,036 KB
testcase_45 AC 159 ms
15,112 KB
testcase_46 AC 182 ms
15,148 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