結果

問題 No.2316 Freight Train
ユーザー Ekiben542Ekiben542
提出日時 2023-05-26 21:30:17
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,796 ms / 2,000 ms
コード長 438 bytes
コンパイル時間 239 ms
コンパイル使用メモリ 12,544 KB
実行使用メモリ 31,960 KB
最終ジャッジ日時 2024-04-27 00:14:05
合計ジャッジ時間 40,703 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 30 ms
10,624 KB
testcase_01 AC 30 ms
10,752 KB
testcase_02 AC 28 ms
10,752 KB
testcase_03 AC 1,720 ms
31,072 KB
testcase_04 AC 947 ms
20,396 KB
testcase_05 AC 683 ms
20,876 KB
testcase_06 AC 223 ms
11,904 KB
testcase_07 AC 1,431 ms
15,924 KB
testcase_08 AC 1,029 ms
31,792 KB
testcase_09 AC 1,297 ms
23,524 KB
testcase_10 AC 1,396 ms
20,544 KB
testcase_11 AC 1,232 ms
28,920 KB
testcase_12 AC 1,463 ms
27,268 KB
testcase_13 AC 1,730 ms
30,712 KB
testcase_14 AC 1,796 ms
30,752 KB
testcase_15 AC 1,754 ms
31,960 KB
testcase_16 AC 1,746 ms
30,756 KB
testcase_17 AC 1,731 ms
30,708 KB
testcase_18 AC 1,722 ms
30,752 KB
testcase_19 AC 1,786 ms
30,752 KB
testcase_20 AC 1,740 ms
31,824 KB
testcase_21 AC 1,722 ms
30,752 KB
testcase_22 AC 1,742 ms
30,624 KB
testcase_23 AC 1,517 ms
30,712 KB
testcase_24 AC 1,590 ms
30,840 KB
testcase_25 AC 1,535 ms
23,764 KB
testcase_26 AC 1,452 ms
23,888 KB
testcase_27 AC 1,305 ms
10,752 KB
testcase_28 AC 29 ms
10,624 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

parent = [i for i in range(N)]
def find(x):
    if parent[x] != x:
        parent[x] = find(parent[x])
    return parent[x]

for i in range(N):
    if P[i] != -1:
        a = find(i)
        b = find(P[i]-1)
        parent[a] = b

for _ in range(Q):
    A, B = map(int, input().split())
    if find(A-1) == find(B-1):
        print("Yes")
    else:
        print("No")
0