結果

問題 No.2316 Freight Train
ユーザー aaaaaaaaaa2230aaaaaaaaaa2230
提出日時 2023-05-26 22:21:07
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 891 ms / 2,000 ms
コード長 909 bytes
コンパイル時間 347 ms
コンパイル使用メモリ 87,144 KB
実行使用メモリ 108,948 KB
最終ジャッジ日時 2023-08-26 12:13:37
合計ジャッジ時間 21,220 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 64 ms
71,580 KB
testcase_01 AC 65 ms
71,360 KB
testcase_02 AC 65 ms
71,200 KB
testcase_03 AC 874 ms
108,520 KB
testcase_04 AC 586 ms
91,836 KB
testcase_05 AC 517 ms
91,104 KB
testcase_06 AC 268 ms
79,164 KB
testcase_07 AC 689 ms
83,384 KB
testcase_08 AC 627 ms
108,928 KB
testcase_09 AC 711 ms
96,540 KB
testcase_10 AC 722 ms
91,808 KB
testcase_11 AC 694 ms
105,228 KB
testcase_12 AC 756 ms
101,708 KB
testcase_13 AC 846 ms
108,928 KB
testcase_14 AC 853 ms
108,692 KB
testcase_15 AC 835 ms
108,948 KB
testcase_16 AC 829 ms
108,864 KB
testcase_17 AC 834 ms
108,936 KB
testcase_18 AC 836 ms
108,684 KB
testcase_19 AC 891 ms
108,900 KB
testcase_20 AC 867 ms
108,876 KB
testcase_21 AC 841 ms
108,768 KB
testcase_22 AC 874 ms
108,656 KB
testcase_23 AC 532 ms
108,780 KB
testcase_24 AC 550 ms
108,824 KB
testcase_25 AC 587 ms
107,028 KB
testcase_26 AC 503 ms
107,132 KB
testcase_27 AC 479 ms
78,556 KB
testcase_28 AC 63 ms
71,368 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class Unionfind:
     
    def __init__(self,n):
        self.uf = [-1]*n
 
    def find(self,x):
        if self.uf[x] < 0:
            return x
        else:
            self.uf[x] = self.find(self.uf[x])
            return self.uf[x]
 
    def same(self,x,y):
        return self.find(x) == self.find(y)
 
    def union(self,x,y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return False
        if self.uf[x] > self.uf[y]:
            x,y = y,x
        self.uf[x] += self.uf[y]
        self.uf[y] = x
        return True
 
    def size(self,x):
        x = self.find(x)
        return -self.uf[x]

n,q = map(int,input().split())
uf = Unionfind(n)
P = list(map(int,input().split()))
for i,p in enumerate(P):
    if p == -1:
        continue
    uf.union(i,p-1)

for _ in range(q):
    a,b = [int(x)-1 for x in input().split()]
    print("Yes" if uf.same(a,b) else "No")
0