結果

問題 No.2290 UnUnion Find
ユーザー lgswdn
提出日時 2023-05-06 20:41:54
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 150 ms / 2,000 ms
コード長 1,062 bytes
コンパイル時間 2,099 ms
コンパイル使用メモリ 197,176 KB
最終ジャッジ日時 2025-02-12 20:37:46
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 46
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define fi first
#define se second
#define eb emplace_back
#define popc __builtin_popcount
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pii> vp;
typedef unsigned long long ull;
typedef long double ld;

int read() {
  int x=0,w=1; char c=getchar(); 
  while(!isdigit(c)) {if(c=='-') w=-1; c=getchar();}
  while(isdigit(c)) {x=x*10+(c-'0'); c=getchar();}
  return x*w;
}

const int N=2e5+9;
int id[N],n,m;
set<int>s;

int find(int x) {return x==id[x]?x:id[x]=find(id[x]);}

signed main() {
  n=read(), m=read();
  rep(i,1,n) id[i]=i, s.insert(i);
  rep(i,1,m) {
    int op=read();
    if(op==1) {
      int x=read(), y=read();
      x=find(x), y=find(y);
      id[y]=x, s.erase(y);
    } else {
      int x=read(); x=find(x);
      if(s.size()==1) puts("-1");
      else {
        auto it=s.begin();
        if((*it)==x) ++it;
        printf("%d\n",*it);
      }
    }
  }
  return 0;
}
0