結果

問題 No.2316 Freight Train
コンテスト
ユーザー KA37RI
提出日時 2023-05-26 21:58:27
言語 Python3
(3.14.3 + numpy 2.4.4 + scipy 1.17.1)
コンパイル:
python3 -mpy_compile _filename_
実行:
python3 _filename_
結果
TLE  
実行時間 -
コード長 565 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 355 ms
コンパイル使用メモリ 20,700 KB
実行使用メモリ 60,580 KB
最終ジャッジ日時 2026-05-30 23:29:26
合計ジャッジ時間 5,205 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other TLE * 1 -- * 25
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

memo = {}

def top(train, etoigne):
  while train[etoigne] != -2:
    etoigne = train[etoigne]
    if etoigne in memo: return top(train, etoigne)
  return etoigne

def solve(N, Q, P, AB):
  ans = []
  for ai, bi in AB:
    if ai not in memo: memo[ai] = top(P, ai)
    if bi not in memo: memo[bi] = top(P, bi)
    ans.append(memo[ai] == memo[bi])
  return ans

N, Q = [int(x) for x in input().split()]
P = [int(x) - 1 for x in input().split()]
AB = [[int(x) - 1 for x in input().split()] for _ in range(Q)]

[print("Yes" if x else "No") for x in solve(N, Q, P, AB)]
0