結果

問題 No.2290 UnUnion Find
ユーザー Ricky_ponRicky_pon
提出日時 2023-05-05 21:27:07
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 210 ms / 2,000 ms
コード長 2,629 bytes
コンパイル時間 1,674 ms
コンパイル使用メモリ 145,756 KB
実行使用メモリ 13,440 KB
最終ジャッジ日時 2024-05-02 15:15:34
合計ジャッジ時間 11,651 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 38 ms
5,376 KB
testcase_03 AC 112 ms
8,320 KB
testcase_04 AC 123 ms
8,448 KB
testcase_05 AC 128 ms
8,448 KB
testcase_06 AC 127 ms
8,344 KB
testcase_07 AC 124 ms
8,448 KB
testcase_08 AC 121 ms
8,576 KB
testcase_09 AC 119 ms
8,448 KB
testcase_10 AC 121 ms
8,344 KB
testcase_11 AC 124 ms
8,576 KB
testcase_12 AC 124 ms
8,448 KB
testcase_13 AC 118 ms
8,320 KB
testcase_14 AC 121 ms
8,448 KB
testcase_15 AC 124 ms
8,320 KB
testcase_16 AC 123 ms
8,448 KB
testcase_17 AC 131 ms
8,448 KB
testcase_18 AC 121 ms
8,448 KB
testcase_19 AC 147 ms
9,216 KB
testcase_20 AC 210 ms
13,440 KB
testcase_21 AC 48 ms
5,376 KB
testcase_22 AC 93 ms
6,008 KB
testcase_23 AC 91 ms
6,132 KB
testcase_24 AC 177 ms
10,752 KB
testcase_25 AC 78 ms
5,504 KB
testcase_26 AC 199 ms
12,160 KB
testcase_27 AC 193 ms
11,392 KB
testcase_28 AC 119 ms
7,656 KB
testcase_29 AC 165 ms
10,240 KB
testcase_30 AC 56 ms
5,376 KB
testcase_31 AC 161 ms
9,600 KB
testcase_32 AC 85 ms
5,484 KB
testcase_33 AC 122 ms
7,444 KB
testcase_34 AC 200 ms
13,440 KB
testcase_35 AC 192 ms
11,904 KB
testcase_36 AC 95 ms
5,760 KB
testcase_37 AC 114 ms
6,940 KB
testcase_38 AC 81 ms
5,608 KB
testcase_39 AC 93 ms
6,272 KB
testcase_40 AC 187 ms
11,648 KB
testcase_41 AC 79 ms
5,376 KB
testcase_42 AC 156 ms
8,832 KB
testcase_43 AC 149 ms
8,576 KB
testcase_44 AC 123 ms
8,472 KB
testcase_45 AC 122 ms
8,448 KB
testcase_46 AC 120 ms
8,448 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