結果

問題 No.2290 UnUnion Find
コンテスト
ユーザー あるふぁ
提出日時 2026-08-27 06:55:59
言語 C++23(gcc16)
(gcc 16.1.0 + boost 1.92.0)
コンパイル:
g++-16 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 317 ms / 2,000 ms
+ 120µs
コード長 4,167 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,372 ms
コンパイル使用メモリ 341,812 KB
実行使用メモリ 25,600 KB
最終ジャッジ日時 2026-08-27 06:56:16
合計ジャッジ時間 15,470 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 46
権限があれば一括ダウンロードができます
コンパイルメッセージ
In file included from main.cpp:6:
/home/linuxbrew/.linuxbrew/Cellar/gcc/16.1.0/include/c++/16/ciso646:49:6: warning: #warning "<ciso646> is not a standard header since C++20, use <version> to detect implementation-specific macros" [-Wcpp]
   49 | #    warning "<ciso646> is not a standard header since C++20, use <version> to detect implementation-specific macros"
      |      ^~~~~~~
In file included from main.cpp:18:
/home/linuxbrew/.linuxbrew/Cellar/gcc/16.1.0/include/c++/16/ccomplex:51:4: warning: #warning "<ccomplex> is deprecated in C++17, use <complex> instead" [-Wcpp]
   51 | #  warning "<ccomplex> is deprecated in C++17, use <complex> instead"
      |    ^~~~~~~
In file included from main.cpp:21:
/home/linuxbrew/.linuxbrew/Cellar/gcc/16.1.0/include/c++/16/cstdalign:50:6: warning: #warning "<cstdalign> is deprecated in C++17, remove the #include" [-Wcpp]
   50 | #    warning "<cstdalign> is deprecated in C++17, remove the #include"
      |      ^~~~~~~
In file included from main.cpp:22:
/home/linuxbrew/.linuxbrew/Cellar/gcc/16.1.0/include/c++/16/cstdbool:50:6: warning: #warning "<cstdbool> is deprecated in C++17, remove the #include" [-Wcpp]
   50 | #    warning "<cstdbool> is deprecated in C++17, remove the #include"
      |      ^~~~~~~
In file included from main.cpp:24:
/home/linuxbrew/.linuxbrew/Cellar/gcc/16.1.0/include/c++/16/ctgmath:50:6: warning: #warning "<ctgmath> is deprecated in C++17, use <complex> or <cmath> instead" [-Wcpp]
   50 | #    warning "<ctgmath> is deprecated in C++17, use <complex> or <cmath> instead"
      |      ^~~~~~~

ソースコード

diff #
raw source code

#include <inttypes.h>
#include <cassert>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>

// #include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i, n) for(int i=0;i<(int)(n);i++)
#define pb push_back
#define pob pop_back
#define eb emplace_back
#define nall(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define yesno(a) cout<<(a?"YES\n":"NO\n")
#define accu accumulate
#define bs binary_search
#define lb lower_bound
#define ub upper_bound
#define yes cout<<"YES\n"
#define no cout<<"NO\n"
using namespace std;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
template<class T> using pq = priority_queue<T>;
template<class T> using pqg = priority_queue<T, vector<T>, greater<T>>;
template<class T> using vec = vector<T>;
template<class T> using vv = vector<vector<T>>;
template<class T> using vvv = vector<vv<T>>;
const ll MOD = 998244353ll;
// const ll MOD = 1000000007ll;

struct dsu{
    vector<int> par;
    vector<int> sz;

    dsu(int n){
        par.resize(n);
        sz.assign(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 merge(int x, int y){
        x = root(x);
        y = root(y);
        if (x == y) return;
        if (sz[x] < sz[y]) swap(x, y);
        par[y] = x;
        sz[x] += sz[y];
        return;
    }

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

    int size(int x){
        return sz[root(x)];
    }
};

void solve();
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(20);
    int t = 1;
    // cin >> t;
    while (t--) solve();
    return 0;
}

void solve(){
    int N, Q, M;
    cin >> N >> Q, M = N;
    dsu G(N);
    vv<int> H(N);
    set<int> I;
    rep(i, N){
        I.insert(i);
        H[i].push_back(i);
    }

    while (Q--){
        int t;
        cin >> t;
        if (t == 1){
            int u, v;
            cin >> u >> v;
            u = G.root(u-1);
            v = G.root(v-1);
            if (G.same(u, v)) continue;
            G.merge(u, v);
            int r = G.root(u);
            if (r == v) swap(u, v);
            if (H[u].size() < H[v].size()) swap(H[u], H[v]);
            for (int x : H[v]) H[u].push_back(x);
            H[v].clear();
            I.erase(v);
            M--;
        }
        else{
            int u;
            cin >> u;
            if (M == 1){
                cout << -1 << endl;
                continue;
            }
            u = G.root(u-1);
            auto it = next(I.find(u));
            if (it == I.end()) it = I.begin();
            cout << H[*it][0]+1 << endl;
        }
    }
}
0