結果
問題 | No.2316 Freight Train |
ユーザー |
|
提出日時 | 2023-05-27 18:12:58 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 81 ms / 2,000 ms |
コード長 | 623 bytes |
コンパイル時間 | 1,941 ms |
コンパイル使用メモリ | 193,572 KB |
最終ジャッジ日時 | 2025-02-13 09:24:09 |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 26 |
ソースコード
#include<bits/stdc++.h> using namespace std; int rt[200005]; int rnk[200005]; int getroot(int n){ if(rt[n]==-1)return n; return rt[n]=getroot(rt[n]); } void unite(int a,int b){ a=getroot(a),b=getroot(b); if(a!=b){ if(rnk[a]>rnk[b])swap(a,b); if(rnk[a]==rnk[b]){ rnk[b]+=1; } rt[a]=b; } } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int N,Q; cin>>N>>Q; fill(rt,rt+N,-1); for(int i=0;i<N;i++){ int P; cin>>P; if(P!=-1){ unite(i,P-1); } } while(Q--){ int A,B; cin>>A>>B; cout<<(getroot(A-1)==getroot(B-1)?"Yes\n":"No\n"); } }