結果

問題 No.2290 UnUnion Find
ユーザー outlineoutline
提出日時 2023-06-14 20:35:15
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 337 ms / 2,000 ms
コード長 2,848 bytes
コンパイル時間 2,672 ms
コンパイル使用メモリ 149,784 KB
実行使用メモリ 13,832 KB
最終ジャッジ日時 2023-09-05 06:40:11
合計ジャッジ時間 20,080 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 35 ms
4,380 KB
testcase_03 AC 135 ms
8,556 KB
testcase_04 AC 137 ms
8,768 KB
testcase_05 AC 139 ms
8,548 KB
testcase_06 AC 137 ms
8,560 KB
testcase_07 AC 134 ms
8,556 KB
testcase_08 AC 135 ms
8,608 KB
testcase_09 AC 136 ms
8,556 KB
testcase_10 AC 133 ms
8,556 KB
testcase_11 AC 134 ms
8,560 KB
testcase_12 AC 134 ms
8,616 KB
testcase_13 AC 131 ms
8,552 KB
testcase_14 AC 132 ms
8,604 KB
testcase_15 AC 136 ms
8,640 KB
testcase_16 AC 130 ms
8,548 KB
testcase_17 AC 135 ms
8,680 KB
testcase_18 AC 130 ms
8,628 KB
testcase_19 AC 221 ms
9,392 KB
testcase_20 AC 336 ms
13,832 KB
testcase_21 AC 46 ms
4,376 KB
testcase_22 AC 102 ms
5,800 KB
testcase_23 AC 102 ms
5,740 KB
testcase_24 AC 280 ms
10,924 KB
testcase_25 AC 81 ms
5,260 KB
testcase_26 AC 326 ms
12,564 KB
testcase_27 AC 289 ms
11,836 KB
testcase_28 AC 155 ms
7,712 KB
testcase_29 AC 274 ms
10,392 KB
testcase_30 AC 55 ms
4,380 KB
testcase_31 AC 240 ms
9,996 KB
testcase_32 AC 83 ms
5,216 KB
testcase_33 AC 155 ms
7,612 KB
testcase_34 AC 337 ms
13,832 KB
testcase_35 AC 316 ms
12,576 KB
testcase_36 AC 90 ms
5,508 KB
testcase_37 AC 135 ms
7,180 KB
testcase_38 AC 89 ms
5,380 KB
testcase_39 AC 109 ms
6,072 KB
testcase_40 AC 302 ms
12,032 KB
testcase_41 AC 78 ms
4,960 KB
testcase_42 AC 221 ms
8,820 KB
testcase_43 AC 202 ms
8,552 KB
testcase_44 AC 137 ms
8,672 KB
testcase_45 AC 138 ms
8,680 KB
testcase_46 AC 158 ms
8,604 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <array>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <memory>
using namespace std;

// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
// #pragma GCC target("avx512f,avx512dq,avx512cd,avx512bw,avx512vl")

using ll = long long;
constexpr int INF = 1001001001;
// constexpr int mod = 1000000007;
constexpr int mod = 998244353;

template<class T>
inline bool chmax(T& x, T y){
    if(x < y){
        x = y;
        return true;
    }
    return false;
}
template<class T>
inline bool chmin(T& x, T y){
    if(x > y){
        x = y;
        return true;
    }
    return false;
}

constexpr int dx[] = {1, 0, -1, 0, 1, 1, -1, -1};
constexpr int dy[] = {0, 1, 0, -1, 1, -1, 1, -1};

struct UnionFind{
    int sz;
    vector<int> par;
    vector<int> rank;

    UnionFind(int n) : sz(n) {
        par.resize(sz);
        rank.assign(sz, 0);
        for(int i = 0; i < sz; ++i){
            par[i] = i;
        }
    }

    int find(int x){
        if(par[x] == x) return x;
        else    return par[x] = find(par[x]);
    }

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

    bool unite(int x, int y){
        x = find(x), y = find(y);
        if(x == y)  return false;
        if(rank[x] < rank[y])   par[x] = y;
        else{
            par[y] = x;
            if(rank[x] == rank[y])  ++rank[x];
        }
        return true;
    }
};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N, Q;
    cin >> N >> Q;
    UnionFind uf(N);
    set<int> st;
    for(int i = 0; i < N; ++i)  st.emplace(i);
    for(int q = 0; q < Q; ++q){
        int t;
        cin >> t;
        if(t == 1){
            int u, v;
            cin >> u >> v;
            --u, --v;
            u = uf.find(u);
            v = uf.find(v);
            st.erase(u);
            st.erase(v);
            uf.unite(u, v);
            st.emplace(uf.find(u));
        }
        else{
            int v;
            cin >> v;
            --v;
            v = uf.find(v);
            if(st.size() == 1)  cout << -1 << '\n';
            else{
                auto it = st.upper_bound(v);
                if(it == st.end()){
                    it = prev(st.find(v));
                }
                cout << *it + 1 << '\n';
            }
        }
    }

    return 0;
}
0