結果

問題 No.2316 Freight Train
ユーザー akasinnakasinn
提出日時 2023-05-26 22:05:23
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 624 ms / 2,000 ms
コード長 658 bytes
コンパイル時間 1,858 ms
コンパイル使用メモリ 174,176 KB
実行使用メモリ 13,304 KB
最終ジャッジ日時 2023-08-26 11:41:58
合計ジャッジ時間 17,923 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 563 ms
12,760 KB
testcase_04 AC 287 ms
8,084 KB
testcase_05 AC 226 ms
8,012 KB
testcase_06 AC 58 ms
4,380 KB
testcase_07 AC 378 ms
5,688 KB
testcase_08 AC 430 ms
13,028 KB
testcase_09 AC 404 ms
9,608 KB
testcase_10 AC 399 ms
8,324 KB
testcase_11 AC 463 ms
12,256 KB
testcase_12 AC 498 ms
11,512 KB
testcase_13 AC 616 ms
13,040 KB
testcase_14 AC 623 ms
13,168 KB
testcase_15 AC 624 ms
13,120 KB
testcase_16 AC 600 ms
12,988 KB
testcase_17 AC 600 ms
13,068 KB
testcase_18 AC 615 ms
13,184 KB
testcase_19 AC 571 ms
13,076 KB
testcase_20 AC 584 ms
13,000 KB
testcase_21 AC 576 ms
13,072 KB
testcase_22 AC 595 ms
13,068 KB
testcase_23 AC 489 ms
13,304 KB
testcase_24 AC 505 ms
12,992 KB
testcase_25 AC 378 ms
4,640 KB
testcase_26 AC 383 ms
4,552 KB
testcase_27 AC 291 ms
4,380 KB
testcase_28 AC 1 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//yukicoder390C
#include<bits/stdc++.h>
using namespace std;
typedef int64_t ll;

int main(){
  int n,q; cin>>n>>q;
  map<int,int> Mp;//前ー後
  vector<int> Head(0);//先頭の番号
  for(int i=1;i<=n;i++){
    int p; cin>>p;
    if(p==-1){
      Head.push_back(i);
    }else{
      Mp[p]=i;
    }
  }
  vector<int> Belong(n+1,0);//先頭の番号を記録
  for(int head:Head){
    int h=head;
    Belong.at(h)=head;
    while(Mp.count(h)){
      h=Mp[h];
      Belong.at(h)=head;
    }
  }
  while(q--){
    int a,b; cin>>a>>b;
    if(Belong.at(a)==Belong.at(b)){
      cout<<"Yes"<<endl;
    }else{
      cout<<"No"<<endl;
    }
    
  }
  return 0;
}
0