結果

問題 No.2316 Freight Train
ユーザー Navier_BoltzmannNavier_Boltzmann
提出日時 2023-07-24 03:28:23
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 516 ms / 2,000 ms
コード長 1,616 bytes
コンパイル時間 923 ms
コンパイル使用メモリ 81,728 KB
実行使用メモリ 105,748 KB
最終ジャッジ日時 2023-10-25 21:52:21
合計ジャッジ時間 16,749 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
55,736 KB
testcase_01 AC 43 ms
55,736 KB
testcase_02 AC 43 ms
55,736 KB
testcase_03 AC 503 ms
105,748 KB
testcase_04 AC 390 ms
91,520 KB
testcase_05 AC 365 ms
91,484 KB
testcase_06 AC 199 ms
78,864 KB
testcase_07 AC 352 ms
81,792 KB
testcase_08 AC 476 ms
101,280 KB
testcase_09 AC 431 ms
95,800 KB
testcase_10 AC 416 ms
91,576 KB
testcase_11 AC 460 ms
102,564 KB
testcase_12 AC 471 ms
99,888 KB
testcase_13 AC 503 ms
101,288 KB
testcase_14 AC 501 ms
101,280 KB
testcase_15 AC 498 ms
101,276 KB
testcase_16 AC 499 ms
101,268 KB
testcase_17 AC 511 ms
101,292 KB
testcase_18 AC 516 ms
101,292 KB
testcase_19 AC 512 ms
101,292 KB
testcase_20 AC 506 ms
101,280 KB
testcase_21 AC 513 ms
101,284 KB
testcase_22 AC 511 ms
101,280 KB
testcase_23 AC 165 ms
99,908 KB
testcase_24 AC 181 ms
99,904 KB
testcase_25 AC 167 ms
105,296 KB
testcase_26 AC 156 ms
105,296 KB
testcase_27 AC 112 ms
76,496 KB
testcase_28 AC 44 ms
55,740 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import *
from itertools import *
from functools import *
from heapq import *
import sys,math
sys.setrecursionlimit(3*10**5)
input = sys.stdin.readline

class DSU:
    def __init__(self, n):
        self._n = n
        self.parent_or_size = [-1] * n

    def merge(self, a, b):
        assert 0 <= a < self._n
        assert 0 <= b < self._n
        x, y = self.leader(a), 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, b):
        assert 0 <= a < self._n
        assert 0 <= b < self._n
        return self.leader(a) == self.leader(b)

    def leader(self, a):
        assert 0 <= a < self._n
        if self.parent_or_size[a] < 0: return a
        self.parent_or_size[a] = self.leader(self.parent_or_size[a])
        return self.parent_or_size[a]

    def size(self, a):
        assert 0 <= a < self._n
        return -self.parent_or_size[self.leader(a)]

    def groups(self):
        leader_buf = [self.leader(i) for i in range(self._n)]
        result = [[] for _ in range(self._n)]
        for i in range(self._n): result[leader_buf[i]].append(i)
        return [r for r in result if r != []]

N,Q = map(int,input().split())
P = list(map(int,input().split()))
D = DSU(N)
for i,p in enumerate(P):
    if p==-1:
        continue
    D.merge(i,p-1)
for _ in range(Q):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    if D.same(a,b):
        print('Yes')
    else:
        print('No')
0