結果

問題 No.2316 Freight Train
ユーザー momo
提出日時 2023-06-02 21:18:19
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 516 ms / 2,000 ms
コード長 492 bytes
コンパイル時間 872 ms
コンパイル使用メモリ 71,380 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-12-28 15:57:14
合計ジャッジ時間 14,033 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

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