結果

問題 No.2316 Freight Train
ユーザー mkawa2mkawa2
提出日時 2023-05-26 21:37:41
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 404 ms / 2,000 ms
コード長 1,681 bytes
コンパイル時間 452 ms
コンパイル使用メモリ 87,132 KB
実行使用メモリ 107,496 KB
最終ジャッジ日時 2023-08-26 10:43:51
合計ジャッジ時間 11,413 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
71,240 KB
testcase_01 AC 71 ms
71,324 KB
testcase_02 AC 72 ms
71,060 KB
testcase_03 AC 367 ms
107,496 KB
testcase_04 AC 266 ms
91,024 KB
testcase_05 AC 247 ms
90,736 KB
testcase_06 AC 180 ms
78,440 KB
testcase_07 AC 295 ms
82,632 KB
testcase_08 AC 299 ms
107,264 KB
testcase_09 AC 321 ms
95,072 KB
testcase_10 AC 307 ms
91,276 KB
testcase_11 AC 326 ms
104,332 KB
testcase_12 AC 340 ms
100,632 KB
testcase_13 AC 391 ms
107,244 KB
testcase_14 AC 398 ms
107,268 KB
testcase_15 AC 404 ms
107,424 KB
testcase_16 AC 400 ms
107,240 KB
testcase_17 AC 366 ms
107,212 KB
testcase_18 AC 391 ms
107,220 KB
testcase_19 AC 400 ms
107,440 KB
testcase_20 AC 402 ms
107,096 KB
testcase_21 AC 392 ms
107,256 KB
testcase_22 AC 387 ms
107,216 KB
testcase_23 AC 270 ms
107,280 KB
testcase_24 AC 290 ms
107,196 KB
testcase_25 AC 220 ms
106,196 KB
testcase_26 AC 201 ms
106,224 KB
testcase_27 AC 157 ms
77,816 KB
testcase_28 AC 72 ms
71,460 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

# sys.setrecursionlimit(200005)
# sys.set_int_max_str_digits(200005)
int1 = lambda x: int(x)-1
pDB = lambda *x: print(*x, end="\n", file=sys.stderr)
p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr)
def II(): return int(sys.stdin.readline())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
def SI(): return sys.stdin.readline().rstrip()

# dij = [(0, 1), (-1, 0), (0, -1), (1, 0)]
dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
inf = (1 << 63)-1
# md = 10**9+7
md = 998244353

class UnionFind:
    def __init__(self, n):
        self._tree = [-1]*n
        # number of connected component
        self.cnt = n

    def root(self, u):
        stack = []
        while self._tree[u] >= 0:
            stack.append(u)
            u = self._tree[u]
        for v in stack: self._tree[v] = u
        return u

    def same(self, u, v):
        return self.root(u) == self.root(v)

    def merge(self, u, v):
        u, v = self.root(u), self.root(v)
        if u == v: return False
        if self._tree[u] > self._tree[v]: u, v = v, u
        self._tree[u] += self._tree[v]
        self._tree[v] = u
        self.cnt -= 1
        return True

    # size of connected component
    def size(self, u):
        return -self._tree[self.root(u)]

n,q=LI()
pp=LI1()
uf=UnionFind(n)
for i,p in enumerate(pp):
    if p==-2:continue
    uf.merge(i,p)

for _ in range(q):
    a,b=LI1()
    print("Yes" if uf.same(a,b) else "No")

0