結果

問題 No.2290 UnUnion Find
ユーザー umezoumezo
提出日時 2023-05-05 22:31:23
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 80 ms / 2,000 ms
コード長 1,351 bytes
コンパイル時間 4,002 ms
コンパイル使用メモリ 207,360 KB
実行使用メモリ 7,752 KB
最終ジャッジ日時 2023-08-15 05:10:30
合計ジャッジ時間 12,011 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 36 ms
4,380 KB
testcase_03 AC 53 ms
5,360 KB
testcase_04 AC 51 ms
5,336 KB
testcase_05 AC 56 ms
5,344 KB
testcase_06 AC 53 ms
5,276 KB
testcase_07 AC 53 ms
5,276 KB
testcase_08 AC 53 ms
5,360 KB
testcase_09 AC 55 ms
5,380 KB
testcase_10 AC 57 ms
5,528 KB
testcase_11 AC 56 ms
5,328 KB
testcase_12 AC 53 ms
5,396 KB
testcase_13 AC 55 ms
5,340 KB
testcase_14 AC 55 ms
5,356 KB
testcase_15 AC 52 ms
5,352 KB
testcase_16 AC 55 ms
5,332 KB
testcase_17 AC 57 ms
5,400 KB
testcase_18 AC 54 ms
5,268 KB
testcase_19 AC 60 ms
5,588 KB
testcase_20 AC 79 ms
7,724 KB
testcase_21 AC 41 ms
4,384 KB
testcase_22 AC 49 ms
4,384 KB
testcase_23 AC 49 ms
4,380 KB
testcase_24 AC 75 ms
6,380 KB
testcase_25 AC 49 ms
4,380 KB
testcase_26 AC 79 ms
7,164 KB
testcase_27 AC 77 ms
6,648 KB
testcase_28 AC 60 ms
4,844 KB
testcase_29 AC 70 ms
6,136 KB
testcase_30 AC 44 ms
4,384 KB
testcase_31 AC 62 ms
5,852 KB
testcase_32 AC 52 ms
4,380 KB
testcase_33 AC 58 ms
4,816 KB
testcase_34 AC 80 ms
7,752 KB
testcase_35 AC 72 ms
6,904 KB
testcase_36 AC 48 ms
4,380 KB
testcase_37 AC 54 ms
4,556 KB
testcase_38 AC 47 ms
4,380 KB
testcase_39 AC 50 ms
4,388 KB
testcase_40 AC 74 ms
6,676 KB
testcase_41 AC 47 ms
4,380 KB
testcase_42 AC 60 ms
5,584 KB
testcase_43 AC 61 ms
5,360 KB
testcase_44 AC 53 ms
5,328 KB
testcase_45 AC 64 ms
5,332 KB
testcase_46 AC 56 ms
5,344 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define ALL(v) v.begin(),v.end()
typedef long long ll;

#include <bits/stdc++.h>
using namespace std;

class Dis{
  public:
  vector<ll> rank,p,siz;
  
  Dis(int s){
    rank.resize(s,0);
    p.resize(s,0);
    siz.resize(s,1);
    rep(i,s) makeSet(i);
  }
  
  void makeSet(int x){
    p[x]=x;
    rank[x]=0;
  }
  
  bool same(int x,int y){
    return findSet(x)==findSet(y);
  }
  
  void unite(int x,int y){
    if(same(x,y)) return;
    link(findSet(x),findSet(y));
  }
  
  void link(int x,int y){
    if(rank[x]>rank[y]){
      p[y]=x;
      siz[x]+=siz[y];
    }
    else{
      p[x]=y;
      siz[y]+=siz[x];
      if(rank[x]==rank[y]) rank[y]++;
    }
  }
  
  int findSet(int x){
    if(x != p[x]) p[x]=findSet(p[x]);
    return p[x];
  }
  
  int size(int x){
    return siz[findSet(x)];
  }
};

int main(){
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);

  int n,q;
  cin>>n>>q;
  Dis ds=Dis(n);
  int r=n-1;
  while(q--){
    int c;
    cin>>c;
    if(c==1){
      int u,v;
      cin>>u>>v;
      u--,v--;
      ds.unite(u,v);
    }
    else{
      int v;
      cin>>v;
      v--;
      if(ds.same(0,r)) cout<<-1<<'\n';
      else if(!ds.same(0,v)) cout<<1<<'\n';
      else cout<<r+1<<'\n';
    }
    if(ds.same(0,r)){
      while(r>0 && ds.same(0,r)) r--;
    }
  }
  
  return 0;
}
0