結果

問題 No.2316 Freight Train
ユーザー ryohei22
提出日時 2023-06-02 15:52:30
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,106 ms / 2,000 ms
コード長 381 bytes
コンパイル時間 1,356 ms
コンパイル使用メモリ 81,792 KB
実行使用メモリ 141,696 KB
最終ジャッジ日時 2024-12-28 15:39:35
合計ジャッジ時間 19,363 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

N, Q = map(int, input().split())
P = tuple(map(int, input().split()))

s = set(range(1, N+1))
for p in P:
	if p in s:
		s.remove(p)

g = [-1] * N
for i in s:
	p = i - 1
	lis = [p]
	while P[p] != -1:
		p = P[p] - 1
		lis.append(p)
	for j in lis:
		g[j] = p

for _ in range(Q):
	A, B = map(lambda x: int(x) - 1, input().split())
	if g[A] == g[B]:
		print('Yes')
	else:
		print('No')
0