結果
問題 | No.2290 UnUnion Find |
ユーザー |
![]() |
提出日時 | 2023-05-05 22:31:23 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 69 ms / 2,000 ms |
コード長 | 1,351 bytes |
コンパイル時間 | 2,267 ms |
コンパイル使用メモリ | 203,620 KB |
最終ジャッジ日時 | 2025-02-12 18:46:12 |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 46 |
ソースコード
#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; }