結果

問題 No.2316 Freight Train
ユーザー kyskys
提出日時 2023-05-26 21:25:00
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 649 ms / 2,000 ms
コード長 2,313 bytes
コンパイル時間 344 ms
コンパイル使用メモリ 87,068 KB
実行使用メモリ 110,780 KB
最終ジャッジ日時 2023-08-26 09:58:41
合計ジャッジ時間 15,403 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 64 ms
71,072 KB
testcase_01 AC 63 ms
71,308 KB
testcase_02 AC 65 ms
71,120 KB
testcase_03 AC 631 ms
109,868 KB
testcase_04 AC 484 ms
92,192 KB
testcase_05 AC 474 ms
92,412 KB
testcase_06 AC 240 ms
80,144 KB
testcase_07 AC 425 ms
86,604 KB
testcase_08 AC 584 ms
105,344 KB
testcase_09 AC 527 ms
97,328 KB
testcase_10 AC 510 ms
92,396 KB
testcase_11 AC 591 ms
106,872 KB
testcase_12 AC 568 ms
103,216 KB
testcase_13 AC 636 ms
105,276 KB
testcase_14 AC 649 ms
105,308 KB
testcase_15 AC 633 ms
105,744 KB
testcase_16 AC 622 ms
105,360 KB
testcase_17 AC 646 ms
105,288 KB
testcase_18 AC 623 ms
105,128 KB
testcase_19 AC 512 ms
105,352 KB
testcase_20 AC 622 ms
105,180 KB
testcase_21 AC 623 ms
105,436 KB
testcase_22 AC 626 ms
105,172 KB
testcase_23 AC 186 ms
106,000 KB
testcase_24 AC 208 ms
106,136 KB
testcase_25 AC 186 ms
110,780 KB
testcase_26 AC 171 ms
110,764 KB
testcase_27 AC 126 ms
79,228 KB
testcase_28 AC 65 ms
71,440 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def main():
    from sys import stdin, setrecursionlimit
    # setrecursionlimit(1000000)
    input = stdin.readline
    def iinput(): return int(input())
    def sinput(): return input().rstrip()
    def i0input(): return int(input()) - 1
    def linput(): return list(input().split())
    def liinput(): return list(map(int, input().split()))
    def miinput(): return map(int, input().split())
    def li0input(): return list(map(lambda x: int(x) - 1, input().split()))
    def mi0input(): return map(lambda x: int(x) - 1, input().split())
    INF = 1000000000000000000
    MOD = 1000000007

    class UnionFindTree:
        def __init__(self, initial_size:int) -> None:
            self.n_nodes = initial_size
            self.parents = [i for i in range(initial_size)]
            self.ranks = [0 for i in range(initial_size)]
            self.size = [1 for i in range(initial_size)]
            self.n_roots = initial_size
        def root(self, n:int) -> int:
            if self.parents[n] == n:
                return n
            else:
                self.parents[n] = self.root(self.parents[n])
                return self.parents[n]
        def same(self, n:int, m:int) -> bool:
            return (self.root(n) == self.root(m))
        def unite(self, n:int, m:int) -> None:
            if self.same(n, m):
                return
            self.n_roots -= 1
            n, m = self.root(n), self.root(m)
            if self.ranks[n] < self.ranks[m]:
                self.parents[n] = m
                self.size[m] += self.size[n]
            else:
                self.parents[m] = n
                self.size[n] += self.size[m]
                if self.ranks[n] == self.ranks[m]:
                    self.ranks[n] += 1
        def get_roots(self) -> set:
            return set([self.root(x) for x in range(self.n_nodes)])
        def count_roots(self) -> int:
            return self.n_roots
        def get_tree_size(self, n:int) -> int:
            return self.size[self.root(n)]

    N, Q = miinput()
    uf = UnionFindTree(N)

    P = li0input()

    for i, p in enumerate(P):
        if p == -2:
            continue
        uf.unite(i, p)
    
    for _ in [0] * Q:
        a, b = mi0input()
        if uf.same(a, b):
            print('Yes')
        else:
            print('No')

main()
0