結果

問題 No.2316 Freight Train
ユーザー 👑 KA37RIKA37RI
提出日時 2023-05-26 22:05:12
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,143 ms / 2,000 ms
コード長 720 bytes
コンパイル時間 162 ms
コンパイル使用メモリ 10,856 KB
実行使用メモリ 66,344 KB
最終ジャッジ日時 2023-08-26 11:40:25
合計ジャッジ時間 24,098 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 18 ms
7,844 KB
testcase_01 AC 16 ms
7,772 KB
testcase_02 AC 17 ms
7,952 KB
testcase_03 AC 1,015 ms
64,444 KB
testcase_04 AC 533 ms
39,024 KB
testcase_05 AC 383 ms
32,836 KB
testcase_06 AC 124 ms
14,436 KB
testcase_07 AC 818 ms
49,428 KB
testcase_08 AC 583 ms
49,472 KB
testcase_09 AC 730 ms
48,048 KB
testcase_10 AC 786 ms
49,560 KB
testcase_11 AC 691 ms
54,272 KB
testcase_12 AC 845 ms
52,956 KB
testcase_13 AC 1,028 ms
65,200 KB
testcase_14 AC 1,015 ms
65,324 KB
testcase_15 AC 1,143 ms
65,392 KB
testcase_16 AC 1,010 ms
65,392 KB
testcase_17 AC 1,028 ms
65,460 KB
testcase_18 AC 1,025 ms
65,288 KB
testcase_19 AC 1,032 ms
65,200 KB
testcase_20 AC 1,034 ms
65,344 KB
testcase_21 AC 1,025 ms
65,272 KB
testcase_22 AC 1,028 ms
65,196 KB
testcase_23 AC 849 ms
60,700 KB
testcase_24 AC 1,020 ms
66,344 KB
testcase_25 AC 987 ms
52,724 KB
testcase_26 AC 847 ms
60,380 KB
testcase_27 AC 679 ms
31,956 KB
testcase_28 AC 16 ms
7,900 KB
権限があれば一括ダウンロードができます

ソースコード

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