結果

問題 No.2316 Freight Train
ユーザー ああいいああいい
提出日時 2023-05-26 21:37:55
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 926 ms / 2,000 ms
コード長 610 bytes
コンパイル時間 307 ms
コンパイル使用メモリ 87,340 KB
実行使用メモリ 108,904 KB
最終ジャッジ日時 2023-08-26 10:46:34
合計ジャッジ時間 22,090 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,364 KB
testcase_01 AC 74 ms
70,828 KB
testcase_02 AC 77 ms
71,088 KB
testcase_03 AC 901 ms
108,392 KB
testcase_04 AC 607 ms
91,668 KB
testcase_05 AC 529 ms
91,780 KB
testcase_06 AC 283 ms
81,112 KB
testcase_07 AC 733 ms
83,224 KB
testcase_08 AC 653 ms
108,744 KB
testcase_09 AC 724 ms
96,140 KB
testcase_10 AC 757 ms
91,436 KB
testcase_11 AC 721 ms
104,868 KB
testcase_12 AC 780 ms
102,088 KB
testcase_13 AC 872 ms
108,884 KB
testcase_14 AC 889 ms
108,808 KB
testcase_15 AC 926 ms
108,600 KB
testcase_16 AC 904 ms
108,676 KB
testcase_17 AC 887 ms
108,676 KB
testcase_18 AC 904 ms
108,572 KB
testcase_19 AC 886 ms
108,568 KB
testcase_20 AC 895 ms
108,492 KB
testcase_21 AC 896 ms
108,680 KB
testcase_22 AC 887 ms
108,800 KB
testcase_23 AC 559 ms
108,512 KB
testcase_24 AC 607 ms
108,904 KB
testcase_25 AC 587 ms
106,876 KB
testcase_26 AC 553 ms
106,948 KB
testcase_27 AC 516 ms
78,604 KB
testcase_28 AC 80 ms
70,972 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

import sys
sys.setrecursionlimit(10 ** 8)
parent= list(range(N + 1))

def find(i):
    if parent[i] == i:return i
    parent[i] = find(parent[i])
    return parent[i]
def unite(i,j):
     I = find(i)
     J = find(j)
     if I == J:return False
     parent[i] = J
     parent[I] = J
     return True
def isSame(i,j):
     return find(i) == find(j)

for i in range(N):
    if P[i] == -1:continue
    unite(i + 1,P[i])

for _ in range(Q):
    a,b = map(int,input().split())
    if isSame(a,b):
        print('Yes')
    else:
        print('No')
0