結果

問題 No.2290 UnUnion Find
ユーザー 入野拳樹
提出日時 2025-07-15 18:54:33
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 568 ms / 2,000 ms
コード長 3,976 bytes
コンパイル時間 1,666 ms
コンパイル使用メモリ 153,644 KB
実行使用メモリ 27,196 KB
最終ジャッジ日時 2025-07-15 18:55:00
合計ジャッジ時間 26,865 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 46
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <set>
#include <cmath>
#include <iomanip>
#include <stack>
#include <queue>
#include <deque>
#include <bitset>
#include <tuple>
#include <map>
#include <functional>
#include <fstream>

#define fi first
#define se second
#define rep(i,n) for(ll i=0;i<(n);i++)
#define rrep(i,n) for(ll i=(n)-1;i>=0;i--)
#define orep(i,n) for(ll i=1;i<=(n);i++)

#define nfor(i,s,n) for(ll i=(s);i<(n);i++)
#define dfor(i,s,n) for(ll i=(s)-1;i>=n;i--)
#define INF 9e18//9223372036854775807

#define all(a)  a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()

#define chmax(x,y) x = max(x,y)
#define chmin(x,y) x = min(x,y)

#define pb push_back
#define pob pop_back

#define vc vector

#define YES cout << "Yes" << endl;
#define NO cout << "No" << endl;
#define YN {cout << "Yes" << endl;}else{cout << "No" << endl;}
#define dame cout << -1 << endl;

#define vc_unique(v) v.erase(unique(v.begin(), v.end()), v.end())
#define vc_remove(v,x) v.erase(remove(v.begin(), v.end(),x), v.end())
#define vc_rotate(v) rotate(v.begin(), v.begin()+1, v.end())

#define pop_cnt(s) ll(popcount(uint64_t(s)))
#define next_p(v) next_permutation(v.begin(),v.end())

#ifndef ONLINE_JUDGE
#define _GLIBCXX_DEBUG
#endif

using namespace std;
using ll = long long;
using ld = long double;
using pll = pair<ll,ll>;
using vvvvvl = vector<vector<vector<vector<vector<ll> > > > >;
using vvvvl = vector<vector<vector<vector<ll> > > >;
using vvvl = vector<vector<vector<ll> > >;
using vvl = vector<vector<ll> >;
using vl = vector<ll>;
using Graph = vector<vector<ll> >;

template<class T> using pq = priority_queue<T,vc<T>,less<T> >;
template<class T> using pq_g = priority_queue<T,vc<T>,greater<T> >;

ll dx[4] = {0,1,0,-1};//vl dx = {1,1,0,-1,-1,-1,0,1};
ll dy[4] = {1,0,-1,0};//vl dy = {0,1,1,1,0,-1,-1,-1};

bool out_grid(ll i, ll j, ll h, ll w){//trueならcontinueする
    return(!(0 <= i && i < h && 0 <= j && j<w));
}

ll MOD = 998244353;

ll modpow(ll x,ll y){
    ll a = x;
    a %= MOD;
    ll cnt = 0;
    ll ans = 1;
    while(y > 0){
        if((1ll << cnt) & y){
            y ^= (1ll << cnt);
            ans *= a;
            ans %= MOD;
        }
        a = a*a;
        a %= MOD;
        cnt++;
    }
    return ans;
}

ll npow(ll x,ll y){
    ll a = x;
    ll cnt = 0;
    ll ans = 1;
    while(y > 0){
        if((1ll << cnt) & y){
            y ^= (1ll << cnt);
            ans *= a;
        }
        a = a*a;
        cnt++;
    }
    return ans;
}

void print(ld x){printf("%.20Lf\n", x);}

//////////////////////////////////////////////////////////////////
vector<ll> par; // 親ノード
vector<ll> Rank; // ランク
vc<vc<ll>> nn;

set<ll> ari;

ll root(ll x) {
    if (par[x] == x) {
        return x;
    }
    else {
        ll r = root(par[x]);
        return par[x] = r;
    }
}

bool issame(ll x, ll y) {
    return root(x) == root(y);
}

bool merge(ll x, ll y) {
    x = root(x); y = root(y);
    if (x == y) return false;
    if (Rank[x] < Rank[y]) swap(x, y);
    if (Rank[x] == Rank[y]) ++Rank[x];
    par[y] = x;
    ari.erase(y);
    rep(i,nn[y].size())nn[x].pb(nn[y][i]);
    return true;
}

int main(){
    ll N,Q;
    cin >> N >> Q;
    par.resize(N); Rank.resize(N,0);nn.resize(N);
    rep(i,N) par[i] = i;
    rep(i,N) nn[i].pb(i);

    rep(i,N)ari.insert(i);

    rep(i,Q){
        ll ty;
        cin >> ty;
        if(ty == 1){
            ll u,v;
            cin >> u >> v;
            u--;v--;
            if(issame(u,v))continue;
            merge(u,v);
        }else if(ty == 2){
            ll x;
            cin >> x;
            x--;
            auto itr = ari.begin();
            bool D = false;
            while (itr != ari.end()) {
                if((*itr) != root(x)){
                    cout << (*itr) + 1 << endl;
                    D = true;
                    break;
                }
                itr++;
            }
            if(!D)dame
        }
    }
}
0