結果

問題 No.2290 UnUnion Find
ユーザー Ricky_ponRicky_pon
提出日時 2023-05-05 21:27:07
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 241 ms / 2,000 ms
コード長 2,629 bytes
コンパイル時間 1,924 ms
コンパイル使用メモリ 144,716 KB
実行使用メモリ 13,192 KB
最終ジャッジ日時 2023-08-15 02:58:00
合計ジャッジ時間 13,074 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 37 ms
4,380 KB
testcase_03 AC 129 ms
8,096 KB
testcase_04 AC 133 ms
8,224 KB
testcase_05 AC 132 ms
8,436 KB
testcase_06 AC 136 ms
8,480 KB
testcase_07 AC 134 ms
8,176 KB
testcase_08 AC 132 ms
8,280 KB
testcase_09 AC 130 ms
8,152 KB
testcase_10 AC 132 ms
8,252 KB
testcase_11 AC 133 ms
8,288 KB
testcase_12 AC 138 ms
8,224 KB
testcase_13 AC 135 ms
8,228 KB
testcase_14 AC 133 ms
8,224 KB
testcase_15 AC 131 ms
8,276 KB
testcase_16 AC 131 ms
8,276 KB
testcase_17 AC 134 ms
8,180 KB
testcase_18 AC 132 ms
8,180 KB
testcase_19 AC 168 ms
8,912 KB
testcase_20 AC 239 ms
13,132 KB
testcase_21 AC 50 ms
4,384 KB
testcase_22 AC 99 ms
5,888 KB
testcase_23 AC 98 ms
5,832 KB
testcase_24 AC 198 ms
10,432 KB
testcase_25 AC 83 ms
5,224 KB
testcase_26 AC 230 ms
12,316 KB
testcase_27 AC 206 ms
11,084 KB
testcase_28 AC 136 ms
7,616 KB
testcase_29 AC 190 ms
9,904 KB
testcase_30 AC 59 ms
4,380 KB
testcase_31 AC 183 ms
9,656 KB
testcase_32 AC 86 ms
5,312 KB
testcase_33 AC 132 ms
7,380 KB
testcase_34 AC 241 ms
13,192 KB
testcase_35 AC 223 ms
11,752 KB
testcase_36 AC 91 ms
5,488 KB
testcase_37 AC 119 ms
6,872 KB
testcase_38 AC 89 ms
5,488 KB
testcase_39 AC 102 ms
5,988 KB
testcase_40 AC 215 ms
11,268 KB
testcase_41 AC 81 ms
5,112 KB
testcase_42 AC 161 ms
8,588 KB
testcase_43 AC 156 ms
8,440 KB
testcase_44 AC 132 ms
8,156 KB
testcase_45 AC 132 ms
8,232 KB
testcase_46 AC 131 ms
8,180 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// #include <bits/stdc++.h>
#include <string.h>

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <ciso646>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <optional>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define rep(i, l, r) for (int i = int(l), rr = int(r); i < rr; ++i)
#define repin(i, l, r) for (int i = int(l), rr = int(r); i <= rr; ++i)
#define rep0(i, n) rep(i, 0, n)
#define rrep(i, l, r) for (int i = int(r) - 1, ll = int(l); i >= ll; --i)
#define rrepin(i, l, r) for (int i = int(r), ll = int(l); i >= ll; --i)
#define rrep0(i, n) rrep(i, 0, n)

#define fi first
#define se second

#include <atcoder/dsu>

#include <algorithm>
#include <cassert>
#include <vector>

namespace rklib {

template <class T>
bool chmax(T &a, const T &b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

template <class T>
bool chmin(T &a, const T &b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}

template <class T>
bool chmin_non_negative(T &a, const T &b) {
    if (a < 0 || a > b) {
        a = b;
        return true;
    }
    return false;
}

template <class T>
T div_floor(T a, T b) {
    if (b < 0) a *= -1, b *= -1;
    return a >= 0 ? a / b : (a + 1) / b - 1;
}

template <class T>
T div_ceil(T a, T b) {
    if (b < 0) a *= -1, b *= -1;
    return a > 0 ? (a - 1) / b + 1 : a / b;
}

}  // namespace rklib


using namespace std;
using namespace rklib;

using lint = long long;
using pii = pair<int, int>;
using pll = pair<lint, lint>;

int main() {
    int n, q;
    scanf("%d%d", &n, &q);
    atcoder::dsu uf(n);
    set<int> roots;
    rep0(i, n) roots.insert(i);
    rep0(qq, q) {
        int type;
        scanf("%d", &type);
        if (type == 1) {
            int a, b;
            scanf("%d%d", &a, &b);
            --a;
            --b;
            roots.erase(uf.leader(a));
            roots.erase(uf.leader(b));
            roots.insert(uf.merge(a, b));
        } else {
            int v;
            scanf("%d", &v);
            --v;
            v = uf.leader(v);
            if (roots.size() == 1) {
                puts("-1");
            } else {
                printf("%d\n", *roots.begin() == v ? *roots.rbegin() + 1
                                                   : *roots.begin() + 1);
            }
        }
    }
}
0