結果

問題 No.2290 UnUnion Find
ユーザー hiro71687khiro71687k
提出日時 2023-05-10 17:12:05
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 450 ms / 2,000 ms
コード長 1,906 bytes
コンパイル時間 5,222 ms
コンパイル使用メモリ 271,404 KB
実行使用メモリ 16,972 KB
最終ジャッジ日時 2024-05-05 04:25:25
合計ジャッジ時間 23,642 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 351 ms
5,592 KB
testcase_03 AC 293 ms
9,600 KB
testcase_04 AC 294 ms
9,600 KB
testcase_05 AC 316 ms
9,600 KB
testcase_06 AC 308 ms
9,600 KB
testcase_07 AC 292 ms
9,600 KB
testcase_08 AC 292 ms
9,600 KB
testcase_09 AC 309 ms
9,472 KB
testcase_10 AC 320 ms
9,600 KB
testcase_11 AC 299 ms
9,600 KB
testcase_12 AC 299 ms
9,600 KB
testcase_13 AC 309 ms
9,472 KB
testcase_14 AC 295 ms
9,472 KB
testcase_15 AC 291 ms
9,472 KB
testcase_16 AC 299 ms
9,600 KB
testcase_17 AC 295 ms
9,600 KB
testcase_18 AC 297 ms
9,600 KB
testcase_19 AC 370 ms
11,716 KB
testcase_20 AC 445 ms
16,972 KB
testcase_21 AC 308 ms
6,212 KB
testcase_22 AC 318 ms
8,580 KB
testcase_23 AC 325 ms
8,708 KB
testcase_24 AC 400 ms
13,752 KB
testcase_25 AC 323 ms
7,960 KB
testcase_26 AC 434 ms
15,332 KB
testcase_27 AC 410 ms
14,268 KB
testcase_28 AC 337 ms
9,708 KB
testcase_29 AC 383 ms
12,984 KB
testcase_30 AC 309 ms
6,460 KB
testcase_31 AC 371 ms
12,120 KB
testcase_32 AC 321 ms
8,012 KB
testcase_33 AC 355 ms
9,480 KB
testcase_34 AC 450 ms
16,928 KB
testcase_35 AC 421 ms
15,276 KB
testcase_36 AC 327 ms
8,236 KB
testcase_37 AC 341 ms
9,832 KB
testcase_38 AC 340 ms
8,056 KB
testcase_39 AC 347 ms
8,932 KB
testcase_40 AC 418 ms
14,568 KB
testcase_41 AC 314 ms
7,680 KB
testcase_42 AC 366 ms
11,192 KB
testcase_43 AC 374 ms
10,964 KB
testcase_44 AC 304 ms
9,472 KB
testcase_45 AC 302 ms
10,816 KB
testcase_46 AC 318 ms
10,812 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
using ll=long long;
using ld=long double;
ld pie=3.141592653589793;
ll inf=14449999999999999;
ll mod=998244353;
struct unionfind
{
    vector<ll>par,siz;
    unionfind(ll n): par(n,-1),siz(n,1){}
    ll root(ll x){
        if (par[x]==-1)
        {
            return x;
        }else{
            return par[x]=root(par[x]);
        }
    }
    bool issame(ll x,ll y){
        return root(x)==root(y);
    }
    bool unite(ll x,ll y){
        x=root(x);
        y=root(y);
        if (x==y)
        {
            return false;
        }
        if (siz[x]<siz[y])
        {
            swap(x,y);
        }
        par[y]=x;
        siz[x]+=siz[y];
        return true;
    }
    ll size(ll x){
        return siz[root(x)];
    }
};
int main(){
    ll n,q;
    cin >> n >> q;
    unionfind uf(n);
    set<ll>s;
    for (ll i = 0; i < n; i++)
    {
        s.insert(i);
    }
    s.insert(-1);
    s.insert(inf);
    vector<ll>ans;
    for (ll i = 0; i < q; i++)
    {
        ll t;
        cin >> t;
        if (t==1)
        {
            ll u,v;
            cin >> u >> v;
            u--,v--;
            if (!uf.issame(u,v))
            {
                s.erase(uf.root(u));
                s.erase(uf.root(v));
                uf.unite(u,v);
                s.insert(uf.root(u));
            }
        }else{
            ll v;
            cin >> v;
            v--;
            auto x=s.lower_bound(uf.root(v));
            auto y=prev(x);
            auto z=next(x);
            if (*(y)!=-1)
            {
                ans.push_back(*(y));
            }else if (*(z)!=inf)
            {
                ans.push_back(*(z));
            }else{
                ans.push_back(-2);
            }
        }
    }
    for (ll i = 0; i < ans.size(); i++)
    {
        cout << ans[i]+1 << endl;
    }
    
}
0