結果

問題 No.2290 UnUnion Find
ユーザー ooaiu
提出日時 2024-11-14 16:10:14
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 281 ms / 2,000 ms
コード長 874 bytes
コンパイル時間 3,546 ms
コンパイル使用メモリ 252,976 KB
実行使用メモリ 6,824 KB
最終ジャッジ日時 2024-11-14 16:10:34
合計ジャッジ時間 20,071 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 46
権限があれば一括ダウンロードができます

ソースコード

diff #

#ifndef LOCAL
#include <bits/stdc++.h>

using namespace std;

#define debug(...) (void(0))
#else
#include "algo/debug.h"
#endif

#include <atcoder/dsu>
void solve() {
    int N, Q; cin >> N >> Q;
    atcoder::dsu uf(N);
    vector<int> memo(N);
    while(Q--) {
        int t; cin >> t;
        if(t == 1) {
            int u, v; cin >> u >> v;
            u--; v--;
            uf.merge(u, v);
        }else {
            int u; cin >> u;
            u--;
            int& cur = memo[uf.leader(u)];
            while(cur < N && uf.same(u, cur)) cur++;
            if(cur != N) {
                cout << cur + 1 << endl;
            }else {
                cout << -1 << endl;
            }
        }//???
    }
}

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