結果

問題 No.2316 Freight Train
ユーザー SSRSSSRS
提出日時 2023-05-26 22:10:35
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 468 ms / 2,000 ms
コード長 782 bytes
コンパイル時間 1,748 ms
コンパイル使用メモリ 172,328 KB
実行使用メモリ 15,752 KB
最終ジャッジ日時 2023-08-26 11:50:50
合計ジャッジ時間 13,319 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 425 ms
14,812 KB
testcase_04 AC 219 ms
9,188 KB
testcase_05 AC 169 ms
8,984 KB
testcase_06 AC 52 ms
4,380 KB
testcase_07 AC 358 ms
6,016 KB
testcase_08 AC 257 ms
15,440 KB
testcase_09 AC 323 ms
11,176 KB
testcase_10 AC 346 ms
9,400 KB
testcase_11 AC 305 ms
14,340 KB
testcase_12 AC 368 ms
13,264 KB
testcase_13 AC 446 ms
15,668 KB
testcase_14 AC 451 ms
15,692 KB
testcase_15 AC 448 ms
15,712 KB
testcase_16 AC 445 ms
15,312 KB
testcase_17 AC 437 ms
15,744 KB
testcase_18 AC 444 ms
15,752 KB
testcase_19 AC 451 ms
15,608 KB
testcase_20 AC 468 ms
15,340 KB
testcase_21 AC 436 ms
15,680 KB
testcase_22 AC 437 ms
15,540 KB
testcase_23 AC 381 ms
15,676 KB
testcase_24 AC 408 ms
15,572 KB
testcase_25 AC 374 ms
9,344 KB
testcase_26 AC 387 ms
9,272 KB
testcase_27 AC 292 ms
4,376 KB
testcase_28 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
int main(){
  int N, Q;
  cin >> N >> Q;
  vector<int> P(N);
  for (int i = 0; i < N; i++){
    cin >> P[i];
    if (P[i] != -1){
      P[i]--;
    }
  }
  vector<vector<int>> c(N);
  for (int i = 0; i < N; i++){
    if (P[i] != -1){
      c[P[i]].push_back(i);
    }
  }
  vector<int> r(N, -1);
  for (int i = 0; i < N; i++){
    if (P[i] == -1){
      queue<int> q;
      q.push(i);
      while (!q.empty()){
        int v = q.front();
        q.pop();
        r[v] = i;
        for (int w : c[v]){
          q.push(w);
        }
      }
    }
  }
  for (int i = 0; i < Q; i++){
    int A, B;
    cin >> A >> B;
    A--;
    B--;
    if (r[A] == r[B]){
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }
  }
}
0