結果

問題 No.2290 UnUnion Find
ユーザー umimelumimel
提出日時 2023-11-20 20:56:59
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 177 ms / 2,000 ms
コード長 2,263 bytes
コンパイル時間 1,794 ms
コンパイル使用メモリ 179,048 KB
実行使用メモリ 14,336 KB
最終ジャッジ日時 2023-11-20 20:57:16
合計ジャッジ時間 16,671 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 36 ms
6,676 KB
testcase_03 AC 108 ms
8,832 KB
testcase_04 AC 109 ms
8,832 KB
testcase_05 AC 110 ms
8,832 KB
testcase_06 AC 111 ms
8,832 KB
testcase_07 AC 109 ms
8,832 KB
testcase_08 AC 117 ms
8,832 KB
testcase_09 AC 115 ms
8,832 KB
testcase_10 AC 109 ms
8,832 KB
testcase_11 AC 105 ms
8,832 KB
testcase_12 AC 106 ms
8,832 KB
testcase_13 AC 109 ms
8,832 KB
testcase_14 AC 135 ms
8,832 KB
testcase_15 AC 124 ms
8,832 KB
testcase_16 AC 107 ms
8,832 KB
testcase_17 AC 106 ms
8,832 KB
testcase_18 AC 103 ms
8,832 KB
testcase_19 AC 121 ms
9,600 KB
testcase_20 AC 177 ms
14,336 KB
testcase_21 AC 43 ms
6,676 KB
testcase_22 AC 72 ms
6,676 KB
testcase_23 AC 73 ms
6,676 KB
testcase_24 AC 150 ms
11,392 KB
testcase_25 AC 62 ms
6,676 KB
testcase_26 AC 164 ms
12,928 KB
testcase_27 AC 163 ms
12,032 KB
testcase_28 AC 115 ms
7,936 KB
testcase_29 AC 140 ms
10,880 KB
testcase_30 AC 48 ms
6,676 KB
testcase_31 AC 131 ms
10,112 KB
testcase_32 AC 64 ms
6,676 KB
testcase_33 AC 98 ms
7,808 KB
testcase_34 AC 174 ms
14,336 KB
testcase_35 AC 167 ms
12,800 KB
testcase_36 AC 66 ms
6,676 KB
testcase_37 AC 87 ms
7,168 KB
testcase_38 AC 65 ms
6,676 KB
testcase_39 AC 75 ms
6,676 KB
testcase_40 AC 160 ms
12,288 KB
testcase_41 AC 59 ms
6,676 KB
testcase_42 AC 130 ms
9,216 KB
testcase_43 AC 117 ms
8,960 KB
testcase_44 AC 108 ms
8,832 KB
testcase_45 AC 110 ms
8,832 KB
testcase_46 AC 112 ms
8,832 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
#define drep(i, cc, n) for (ll i = (cc); i <= (n); ++i)
#define rep(i, n) drep(i, 0, n - 1)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define fi first
#define se second
mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count());
const ll MOD1000000007 = 1000000007;
const ll MOD998244353 = 998244353;
const ll MOD[3] = {999727999, 1070777777, 1000000007};
const ll LINF = 1LL << 60LL;
const int IINF = (1 << 30) - 1;


struct union_find{
    vector<int> par;
    vector<int> siz;
    
    union_find(int n) : par(n), siz(n, 1){
        for(int i=0; i<n; i++) par[i] = i;
    }
    
    int root(int x){
        if (par[x] == x) return x;
        return par[x] = root(par[x]);
    }
 
    void unite(int x, int y){
        int rx = root(x);
        int ry = root(y);
        if (rx == ry) return;
        if (siz[rx] < siz[ry]) swap(rx, ry);
        siz[rx] += siz[ry];
        par[ry] = rx;
    }
 
    bool same(int x, int y){
        int rx = root(x);
        int ry = root(y);
        return rx == ry;
    }
 
    int size(int x){
        return siz[root(x)];
    }
};

void solve(){
    ll n, q; cin >> n >> q;
    union_find uf(n);
    set<ll> st;
    rep(i, n) st.insert(i); 
    while(q--){
        ll type; cin >> type;
        if(type==1){
            ll u, v; cin >> u >> v;
            u--; v--;
            if(!uf.same(u, v)){
                ll ru = uf.root(u);
                ll rv = uf.root(v);

                uf.unite(ru, rv);
                if(uf.root(ru)==ru){
                    st.erase(rv);
                }else{
                    st.erase(ru);
                }
            }
        }
        if(type==2){
            ll v; cin >> v;
            v--;
            ll r = uf.root(v);
            if(uf.size(v)==n){
                cout << -1 << '\n';
            }else{
                if(*st.begin() == r){
                    cout << *st.rbegin()+1 << '\n';
                }else{
                    cout << *st.begin()+1 << '\n';
                }
            }
        }
    }
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    
    int T=1;
    //cin >> T;
    while(T--) solve();
}
0