結果

問題 No.2290 UnUnion Find
ユーザー GandalfrGandalfr
提出日時 2023-09-30 01:31:48
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 438 ms / 2,000 ms
コード長 3,992 bytes
コンパイル時間 2,351 ms
コンパイル使用メモリ 213,064 KB
実行使用メモリ 13,784 KB
最終ジャッジ日時 2023-09-30 01:32:14
合計ジャッジ時間 23,928 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 330 ms
4,376 KB
testcase_03 AC 342 ms
9,508 KB
testcase_04 AC 319 ms
9,204 KB
testcase_05 AC 319 ms
9,100 KB
testcase_06 AC 338 ms
9,292 KB
testcase_07 AC 313 ms
8,764 KB
testcase_08 AC 310 ms
9,508 KB
testcase_09 AC 333 ms
8,928 KB
testcase_10 AC 319 ms
9,036 KB
testcase_11 AC 307 ms
9,472 KB
testcase_12 AC 336 ms
9,028 KB
testcase_13 AC 314 ms
9,156 KB
testcase_14 AC 321 ms
9,136 KB
testcase_15 AC 341 ms
9,464 KB
testcase_16 AC 319 ms
8,868 KB
testcase_17 AC 319 ms
9,616 KB
testcase_18 AC 330 ms
9,032 KB
testcase_19 AC 353 ms
9,468 KB
testcase_20 AC 399 ms
13,784 KB
testcase_21 AC 345 ms
4,376 KB
testcase_22 AC 332 ms
5,976 KB
testcase_23 AC 337 ms
5,660 KB
testcase_24 AC 419 ms
10,896 KB
testcase_25 AC 334 ms
5,104 KB
testcase_26 AC 394 ms
12,556 KB
testcase_27 AC 388 ms
11,736 KB
testcase_28 AC 342 ms
7,464 KB
testcase_29 AC 395 ms
10,368 KB
testcase_30 AC 330 ms
4,376 KB
testcase_31 AC 357 ms
9,552 KB
testcase_32 AC 352 ms
5,160 KB
testcase_33 AC 343 ms
7,548 KB
testcase_34 AC 417 ms
13,780 KB
testcase_35 AC 429 ms
12,196 KB
testcase_36 AC 331 ms
5,392 KB
testcase_37 AC 347 ms
6,668 KB
testcase_38 AC 337 ms
5,396 KB
testcase_39 AC 346 ms
6,076 KB
testcase_40 AC 438 ms
12,088 KB
testcase_41 AC 332 ms
4,864 KB
testcase_42 AC 363 ms
8,760 KB
testcase_43 AC 370 ms
8,608 KB
testcase_44 AC 331 ms
9,028 KB
testcase_45 AC 379 ms
8,612 KB
testcase_46 AC 361 ms
8,692 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#line 1 "playspace/main.cpp"
#include <bits/stdc++.h>
#line 3 "library/gandalfr/data_structure/union_find.hpp"

#line 6 "library/gandalfr/data_structure/union_find.hpp"

class union_find {
  private:
    int N;
    mutable std::vector<int> par;
    std::vector<int> nxt;
    int group_num; // 集合の数

  public:
    union_find() : N(0), group_num(0) {}
    union_find(int n) : N(n), par(n, -1), nxt(n), group_num(n) {
        std::iota(nxt.begin(), nxt.end(), 0);
    }

    /**
     * @brief 頂点を n 個に増やす
     * @attention 小さくはできない
     */
    void expand(int n) {
        if (n <= N)
            return;
        par.resize(n, -1);
        nxt.resize(n);
        for (int i = N; i < n; ++i)
            nxt[i] = i;
        group_num += n - N;
        N = n;
    }

    int leader(int x) const {
        return (par[x] < 0 ? x : par[x] = leader(par[x]));
    }

    bool same(int x, int y) const { return leader(x) == leader(y); }

    bool merge(int x, int y) {
        if ((x = leader(x)) == (y = leader(y)))
            return false;
        if (-par[x] > -par[y])
            std::swap(x, y);

        par[x] += par[y];
        par[y] = x;
        std::swap(nxt[x], nxt[y]);
        group_num--;
        return true;
    }

    /**
     * @brief x の属するグループのサイズを返す
     */
    int size(int x) const { return -par[leader(x)]; }

    /**
     * @brief すべてのノードの数
     */
    int size() const { return N; }

    std::vector<int> group_containing_node(int x) const {
        std::vector<int> ret{x};
        for (int cu = nxt[x]; cu != ret[0]; cu = nxt[cu])
            ret.push_back(cu);
        return ret;
    }

    int count_groups() const { return group_num; }

    std::vector<std::vector<int>> all_groups() const {
        std::vector<std::vector<int>> result;
        result.reserve(group_num);
        std::vector<bool> used(N, false);
        for (int i = 0; i < N; ++i) {
            if (!used[i]) {
                result.emplace_back(group_containing_node(i));
                for (int x : result.back()) {
                    used[x] = true;
                }
            }
        }
        return result;
    }
};
#line 3 "playspace/main.cpp"
using namespace std;
using ll = long long;
const int INF = 1001001001;
const ll INFLL = 1001001001001001001;
const ll MOD  = 1000000007;
const ll _MOD = 998244353;
#define rep(i, j, n) for(ll i = (ll)(j); i < (ll)(n); i++)
#define rrep(i, j, n) for(ll i = (ll)(n-1); i >= (ll)(j); i--)
#define all(a) (a).begin(),(a).end()
#define debug(a) std::cerr << #a << ": " << a << std::endl
#define LF cout << endl
#ifdef ENABLE_MULTI_FOR
#define mrep(it, mr) for(multi_iter it(mr); !it.fin(); ++it)
#endif
template<typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }
template<typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }
void Yes(bool ok){ std::cout << (ok ? "Yes" : "No") << std::endl; }

int main(void){

    int N, Q;
    cin >> N >> Q;
    union_find G(N);
    set<int> st;
    rep(i,0,N) {st.insert(i);}
    rep(i,0,Q) {
        int q;
        cin >> q;
        if (q == 1) {
            int u, v;
            cin >> u >> v;
            u--, v--;
            if (G.same(u, v)) {
                continue;
            }

            u = G.leader(u);
            v = G.leader(v);
            if (G.size(u) > G.size(v)) {
                swap(u, v);
                
            }
            G.merge(u, v);
            st.erase(v);
        } else {
            int x;
            cin >> x;
            x--;
            x = G.leader(x);

            if (st.size() == 1) {
                cout << -1 << endl;
            } else {
                auto it = st.begin();
                if (*it != x) {
                    cout << *it + 1 << endl;
                } else {
                    cout << *++it + 1 << endl;
                }
            }
        }
    }



}
0