結果

問題 No.2316 Freight Train
ユーザー 👑 KA37RI
提出日時 2023-05-26 22:05:12
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 1,150 ms / 2,000 ms
コード長 720 bytes
コンパイル時間 269 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 68,960 KB
最終ジャッジ日時 2024-12-25 07:07:03
合計ジャッジ時間 26,528 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

memo = {}

def top(train, etoigne):
  forward = []
  while train[etoigne] != -2:
    forward.append(etoigne)
    etoigne = train[etoigne]
    if etoigne in memo:
      ans = memo[etoigne]
      for e in forward:
        memo[e] = ans
      return ans
  ans = etoigne
  for e in forward:
    memo[e] = ans
  return ans

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