結果

問題 No.2316 Freight Train
ユーザー kemunikukemuniku
提出日時 2023-05-26 22:08:00
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 844 ms / 2,000 ms
コード長 965 bytes
コンパイル時間 256 ms
コンパイル使用メモリ 87,152 KB
実行使用メモリ 110,496 KB
最終ジャッジ日時 2023-08-26 11:45:45
合計ジャッジ時間 20,743 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 62 ms
71,200 KB
testcase_01 AC 68 ms
71,392 KB
testcase_02 AC 67 ms
71,504 KB
testcase_03 AC 822 ms
110,332 KB
testcase_04 AC 581 ms
92,376 KB
testcase_05 AC 505 ms
92,464 KB
testcase_06 AC 268 ms
79,344 KB
testcase_07 AC 680 ms
83,544 KB
testcase_08 AC 618 ms
110,272 KB
testcase_09 AC 687 ms
97,516 KB
testcase_10 AC 724 ms
92,644 KB
testcase_11 AC 684 ms
106,648 KB
testcase_12 AC 738 ms
103,028 KB
testcase_13 AC 843 ms
110,344 KB
testcase_14 AC 844 ms
110,440 KB
testcase_15 AC 821 ms
110,284 KB
testcase_16 AC 839 ms
110,476 KB
testcase_17 AC 837 ms
110,484 KB
testcase_18 AC 812 ms
110,440 KB
testcase_19 AC 829 ms
110,340 KB
testcase_20 AC 836 ms
110,352 KB
testcase_21 AC 813 ms
110,368 KB
testcase_22 AC 836 ms
110,496 KB
testcase_23 AC 487 ms
110,384 KB
testcase_24 AC 511 ms
110,152 KB
testcase_25 AC 503 ms
108,316 KB
testcase_26 AC 492 ms
108,496 KB
testcase_27 AC 436 ms
78,476 KB
testcase_28 AC 62 ms
71,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind():
            
    def __init__(self, n):
        self.count = n
        self.par = [-1]*n
        self.siz = [1]*n
    def root(self,x):
        if(self.par[x] == -1):
            return x
        else:
            self.par[x] = self.root(self.par[x])
            return self.par[x]
    def issame(self,x,y):
        return self.root(x) == self.root(y)
    def unite(self,x,y):
        x = self.root(x)
        y = self.root(y)
        if(x == y):
            return False
        if(self.siz[x]<self.siz[y]):
            x,y = y,x
        self.par[y] = x
        self.siz[x] += self.siz[y]
        self.count -= 1
        return True
    def size(self,x):
        return self.siz[self.root(x)]
N,Q = map(int,input().split())
P = list(map(int,input().split()))
uf = UnionFind(N)
for i in range(N):
    if P[i] != -1:
        uf.unite(i,P[i]-1)

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