結果

問題 No.2316 Freight Train
ユーザー Nzt3Nzt3
提出日時 2023-05-27 18:12:58
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 75 ms / 2,000 ms
コード長 623 bytes
コンパイル時間 1,854 ms
コンパイル使用メモリ 197,784 KB
実行使用メモリ 5,068 KB
最終ジャッジ日時 2023-08-26 22:51:36
合計ジャッジ時間 6,123 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 72 ms
4,832 KB
testcase_04 AC 36 ms
4,380 KB
testcase_05 AC 28 ms
4,380 KB
testcase_06 AC 8 ms
4,380 KB
testcase_07 AC 46 ms
4,376 KB
testcase_08 AC 51 ms
4,928 KB
testcase_09 AC 51 ms
4,432 KB
testcase_10 AC 49 ms
4,376 KB
testcase_11 AC 55 ms
4,952 KB
testcase_12 AC 62 ms
4,620 KB
testcase_13 AC 75 ms
4,928 KB
testcase_14 AC 74 ms
4,904 KB
testcase_15 AC 74 ms
4,944 KB
testcase_16 AC 74 ms
4,996 KB
testcase_17 AC 74 ms
4,904 KB
testcase_18 AC 74 ms
4,940 KB
testcase_19 AC 75 ms
4,904 KB
testcase_20 AC 74 ms
5,004 KB
testcase_21 AC 75 ms
5,068 KB
testcase_22 AC 74 ms
4,900 KB
testcase_23 AC 52 ms
4,376 KB
testcase_24 AC 62 ms
4,376 KB
testcase_25 AC 55 ms
4,380 KB
testcase_26 AC 52 ms
4,376 KB
testcase_27 AC 28 ms
4,376 KB
testcase_28 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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");
  }
}
0