結果

問題 No.2290 UnUnion Find
ユーザー t98slidert98slider
提出日時 2023-02-15 11:33:15
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 53 ms / 2,000 ms
コード長 666 bytes
コンパイル時間 3,954 ms
コンパイル使用メモリ 229,788 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-09-22 23:29:53
合計ジャッジ時間 11,158 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 46
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using ll = long long;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int N, Q;
    cin >> N >> Q;
    assert(1 <= N && N <= 200000);
    assert(1 <= Q && Q <= 200000);
    atcoder::dsu uf(N);
    int l = 0, r = N - 1;
    while(Q--){
        int type, u, v;
        cin >> type;
        if(type == 1){
            cin >> u >> v;
            uf.merge(--u, --v);
            while(l != uf.leader(l)) l++;
            while(r != uf.leader(r)) r--;
        }else{
            cin >> v;
            cout << (l == r ? -1 : (uf.same(--v, r) ? l + 1 : r + 1)) << '\n';
        }
    }
}
0