結果

問題 No.2316 Freight Train
ユーザー momomomo
提出日時 2023-06-02 21:18:19
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 448 ms / 2,000 ms
コード長 492 bytes
コンパイル時間 575 ms
コンパイル使用メモリ 70,932 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-28 02:30:48
合計ジャッジ時間 12,377 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 434 ms
4,376 KB
testcase_04 AC 234 ms
4,380 KB
testcase_05 AC 163 ms
4,380 KB
testcase_06 AC 56 ms
4,380 KB
testcase_07 AC 373 ms
4,380 KB
testcase_08 AC 242 ms
4,376 KB
testcase_09 AC 324 ms
4,380 KB
testcase_10 AC 354 ms
4,380 KB
testcase_11 AC 295 ms
4,380 KB
testcase_12 AC 361 ms
4,376 KB
testcase_13 AC 436 ms
4,376 KB
testcase_14 AC 437 ms
4,376 KB
testcase_15 AC 435 ms
4,376 KB
testcase_16 AC 448 ms
4,380 KB
testcase_17 AC 433 ms
4,380 KB
testcase_18 AC 431 ms
4,384 KB
testcase_19 AC 429 ms
4,380 KB
testcase_20 AC 437 ms
4,380 KB
testcase_21 AC 435 ms
4,380 KB
testcase_22 AC 435 ms
4,380 KB
testcase_23 AC 398 ms
4,380 KB
testcase_24 AC 431 ms
4,384 KB
testcase_25 AC 412 ms
4,384 KB
testcase_26 AC 400 ms
4,380 KB
testcase_27 AC 321 ms
4,384 KB
testcase_28 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
  int n, q;
  cin >> n >> q;

  vector<int> p(n + 1);
  for (int i = 1; i <= n; ++i) {
    cin >> p[i];
  }

  auto f = [&](int k) {
    int t = k;
    while (p[t] > 0) {
      t = p[t];
    }
    while (p[k] > 0) {
      int k2 = k;
      k = p[k];
      p[k2] = t;
    }
    return t;
  };

  while (q--) {
    int a, b;
    cin >> a >> b;

    cout << (f(a) == f(b)? "Yes": "No") << '\n';
  }
}
0