結果

問題 No.2316 Freight Train
ユーザー とろちゃとろちゃ
提出日時 2023-05-26 21:32:34
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 506 ms / 2,000 ms
コード長 2,706 bytes
コンパイル時間 779 ms
コンパイル使用メモリ 86,896 KB
実行使用メモリ 118,560 KB
最終ジャッジ日時 2023-08-26 10:31:37
合計ジャッジ時間 15,014 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 139 ms
79,956 KB
testcase_01 AC 139 ms
79,668 KB
testcase_02 AC 138 ms
79,840 KB
testcase_03 AC 493 ms
114,596 KB
testcase_04 AC 398 ms
103,224 KB
testcase_05 AC 378 ms
99,144 KB
testcase_06 AC 286 ms
88,040 KB
testcase_07 AC 428 ms
108,036 KB
testcase_08 AC 408 ms
113,944 KB
testcase_09 AC 412 ms
108,820 KB
testcase_10 AC 431 ms
109,888 KB
testcase_11 AC 446 ms
110,860 KB
testcase_12 AC 435 ms
108,020 KB
testcase_13 AC 470 ms
114,836 KB
testcase_14 AC 471 ms
115,056 KB
testcase_15 AC 495 ms
114,736 KB
testcase_16 AC 506 ms
114,820 KB
testcase_17 AC 475 ms
114,932 KB
testcase_18 AC 494 ms
114,836 KB
testcase_19 AC 496 ms
115,088 KB
testcase_20 AC 500 ms
114,796 KB
testcase_21 AC 492 ms
115,040 KB
testcase_22 AC 469 ms
114,828 KB
testcase_23 AC 289 ms
114,856 KB
testcase_24 AC 303 ms
114,728 KB
testcase_25 AC 291 ms
118,116 KB
testcase_26 AC 281 ms
118,560 KB
testcase_27 AC 235 ms
103,096 KB
testcase_28 AC 137 ms
80,152 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# import pypyjit;pypyjit.set_param("max_unroll_recursion=-1")
# from bisect import *
# from collections import *
# from heapq import *
# from itertools import *
# from math import *
# from datetime import *
# from decimal import*
# from string import ascii_lowercase,ascii_uppercase
# import numpy as np
import sys
import os

# sys.setrecursionlimit(10**6)
INF = 10**18
MOD = 998244353
# MOD = 10**9 + 7
File = open("input.txt", "r") if os.path.exists("input.txt") else sys.stdin


def input():
    return File.readline()[:-1]


# ///////////////////////////////////////////////////////////////////////////
import typing


class DSU:
    """
    Implement (union by size) + (path halving)

    Reference:
    Zvi Galil and Giuseppe F. Italiano,
    Data structures and algorithms for disjoint set union problems
    """

    def __init__(self, n: int = 0) -> None:
        self._n = n
        self.parent_or_size = [-1] * n

    def merge(self, a: int, b: int) -> int:
        assert 0 <= a < self._n
        assert 0 <= b < self._n

        x = self.leader(a)
        y = self.leader(b)

        if x == y:
            return x

        if -self.parent_or_size[x] < -self.parent_or_size[y]:
            x, y = y, x

        self.parent_or_size[x] += self.parent_or_size[y]
        self.parent_or_size[y] = x

        return x

    def same(self, a: int, b: int) -> bool:
        assert 0 <= a < self._n
        assert 0 <= b < self._n

        return self.leader(a) == self.leader(b)

    def leader(self, a: int) -> int:
        assert 0 <= a < self._n

        parent = self.parent_or_size[a]
        while parent >= 0:
            if self.parent_or_size[parent] < 0:
                return parent
            self.parent_or_size[a], a, parent = (
                self.parent_or_size[parent],
                self.parent_or_size[parent],
                self.parent_or_size[self.parent_or_size[parent]],
            )

        return a

    def size(self, a: int) -> int:
        assert 0 <= a < self._n

        return -self.parent_or_size[self.leader(a)]

    def groups(self) -> typing.List[typing.List[int]]:
        leader_buf = [self.leader(i) for i in range(self._n)]

        result: typing.List[typing.List[int]] = [[] for _ in range(self._n)]
        for i in range(self._n):
            result[leader_buf[i]].append(i)

        return list(filter(lambda r: r, result))


N, Q = map(int, input().split())
P = list(map(int, input().split()))
query = [list(map(int, input().split())) for _ in range(Q)]
uf = DSU(N + 1)

for i, j in enumerate(P):
    if j == -1:
        continue
    uf.merge(i + 1, j)

for i, j in query:
    if uf.same(i, j):
        print("Yes")
    else:
        print("No")
0