結果
| 問題 | 
                            No.2316 Freight Train
                             | 
                    
| コンテスト | |
| ユーザー | 
                             | 
                    
| 提出日時 | 2023-05-26 21:55:53 | 
| 言語 | Python3  (3.13.1 + numpy 2.2.1 + scipy 1.14.1)  | 
                    
| 結果 | 
                             
                                TLE
                                 
                             
                            
                         | 
                    
| 実行時間 | - | 
| コード長 | 515 bytes | 
| コンパイル時間 | 628 ms | 
| コンパイル使用メモリ | 12,416 KB | 
| 実行使用メモリ | 67,012 KB | 
| 最終ジャッジ日時 | 2024-12-25 06:37:00 | 
| 合計ジャッジ時間 | 65,761 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge2 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 7 TLE * 19 | 
ソースコード
def top(train, etoigne):
  while train[etoigne] != -2:
    etoigne = train[etoigne]
  return etoigne
def solve(N, Q, P, AB):
  ans = []
  memo = {}
  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)]