結果

問題 No.2290 UnUnion Find
ユーザー hiro71687khiro71687k
提出日時 2023-05-10 17:10:18
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,888 bytes
コンパイル時間 5,195 ms
コンパイル使用メモリ 270,992 KB
実行使用メモリ 17,188 KB
最終ジャッジ日時 2024-05-05 04:24:27
合計ジャッジ時間 26,010 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 328 ms
5,532 KB
testcase_03 AC 307 ms
9,600 KB
testcase_04 AC 307 ms
9,600 KB
testcase_05 AC 299 ms
9,600 KB
testcase_06 AC 300 ms
9,600 KB
testcase_07 AC 294 ms
9,600 KB
testcase_08 AC 300 ms
9,600 KB
testcase_09 AC 301 ms
9,600 KB
testcase_10 AC 320 ms
9,600 KB
testcase_11 AC 318 ms
9,600 KB
testcase_12 AC 320 ms
9,472 KB
testcase_13 AC 332 ms
9,600 KB
testcase_14 AC 303 ms
10,368 KB
testcase_15 AC 302 ms
9,472 KB
testcase_16 AC 302 ms
9,600 KB
testcase_17 AC 297 ms
9,600 KB
testcase_18 AC 302 ms
9,600 KB
testcase_19 WA -
testcase_20 AC 427 ms
16,932 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 425 ms
13,624 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 431 ms
14,524 KB
testcase_28 WA -
testcase_29 AC 407 ms
12,984 KB
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 AC 487 ms
17,188 KB
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 AC 418 ms
14,696 KB
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 AC 327 ms
9,600 KB
testcase_45 AC 344 ms
10,816 KB
testcase_46 AC 318 ms
10,688 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(u);
                s.erase(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