結果

問題 No.2316 Freight Train
ユーザー ntudantuda
提出日時 2023-05-27 11:25:17
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 960 ms / 2,000 ms
コード長 619 bytes
コンパイル時間 788 ms
コンパイル使用メモリ 86,156 KB
実行使用メモリ 109,064 KB
最終ジャッジ日時 2023-08-26 19:41:44
合計ジャッジ時間 23,061 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 68 ms
71,420 KB
testcase_01 AC 68 ms
71,276 KB
testcase_02 AC 70 ms
71,344 KB
testcase_03 AC 896 ms
108,612 KB
testcase_04 AC 614 ms
91,384 KB
testcase_05 AC 543 ms
91,480 KB
testcase_06 AC 257 ms
78,852 KB
testcase_07 AC 728 ms
83,268 KB
testcase_08 AC 688 ms
108,912 KB
testcase_09 AC 736 ms
95,912 KB
testcase_10 AC 728 ms
91,172 KB
testcase_11 AC 732 ms
105,132 KB
testcase_12 AC 793 ms
101,896 KB
testcase_13 AC 912 ms
108,932 KB
testcase_14 AC 924 ms
108,668 KB
testcase_15 AC 942 ms
109,040 KB
testcase_16 AC 933 ms
108,856 KB
testcase_17 AC 901 ms
109,020 KB
testcase_18 AC 907 ms
108,700 KB
testcase_19 AC 912 ms
108,824 KB
testcase_20 AC 960 ms
109,064 KB
testcase_21 AC 912 ms
108,948 KB
testcase_22 AC 955 ms
108,784 KB
testcase_23 AC 527 ms
108,564 KB
testcase_24 AC 570 ms
108,704 KB
testcase_25 AC 550 ms
106,896 KB
testcase_26 AC 497 ms
106,860 KB
testcase_27 AC 461 ms
78,340 KB
testcase_28 AC 66 ms
71,360 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def find(x):
    if P[x] == x: return x
    else:
        P[x] = find(P[x]) #経路圧縮
        return P[x]
        # return find(P[x]) #経路圧縮なし

def same(x,y):
    return find(x) == find(y)

def unite(x,y):
    x = find(x)
    y = find(y)
    if x == y: return 0
    if x > y: x,y = y,x
    P[y] = x

N,Q = map(int,input().split())
PI = list(map(int,input().split()))
P = [i for i in range(N+1)]

for i,p in enumerate(PI,start = 1):
    if p == -1:
        continue
    unite(i,p)

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