結果

問題 No.2316 Freight Train
ユーザー flygonflygon
提出日時 2023-05-26 21:25:06
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 521 ms / 2,000 ms
コード長 1,260 bytes
コンパイル時間 253 ms
コンパイル使用メモリ 87,104 KB
実行使用メモリ 108,976 KB
最終ジャッジ日時 2023-08-26 10:01:12
合計ジャッジ時間 12,850 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 83 ms
71,336 KB
testcase_01 AC 83 ms
71,764 KB
testcase_02 AC 83 ms
71,704 KB
testcase_03 AC 489 ms
108,144 KB
testcase_04 AC 377 ms
93,444 KB
testcase_05 AC 352 ms
93,276 KB
testcase_06 AC 218 ms
80,764 KB
testcase_07 AC 347 ms
83,192 KB
testcase_08 AC 449 ms
108,480 KB
testcase_09 AC 442 ms
97,444 KB
testcase_10 AC 404 ms
93,000 KB
testcase_11 AC 451 ms
104,676 KB
testcase_12 AC 479 ms
101,460 KB
testcase_13 AC 521 ms
108,640 KB
testcase_14 AC 486 ms
108,432 KB
testcase_15 AC 484 ms
108,312 KB
testcase_16 AC 479 ms
108,520 KB
testcase_17 AC 496 ms
108,204 KB
testcase_18 AC 492 ms
108,252 KB
testcase_19 AC 492 ms
108,328 KB
testcase_20 AC 482 ms
108,668 KB
testcase_21 AC 482 ms
108,592 KB
testcase_22 AC 488 ms
108,512 KB
testcase_23 AC 183 ms
108,948 KB
testcase_24 AC 200 ms
108,976 KB
testcase_25 AC 187 ms
107,428 KB
testcase_26 AC 179 ms
107,396 KB
testcase_27 AC 136 ms
77,640 KB
testcase_28 AC 80 ms
71,432 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(5*10**5)
input = sys.stdin.readline
from collections import defaultdict, deque, Counter
from heapq import heappop, heappush
from bisect import bisect_left, bisect_right
from math import gcd
from collections import defaultdict


class UnionFind:
    def __init__(self, n):
        self.n = n
        self.p = [-1] * (n+1)

    def find(self, x):
        if self.p[x] < 0:
            return x
        else:
            self.p[x] = self.find(self.p[x])
            return self.p[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        if self.p[x] > self.p[y]:
            x, y = y, x
        self.p[x] += self.p[y]
        self.p[y] = x

    def same(self, a, b):
        return self.find(a) == self.find(b)

    def group(self):
        d = defaultdict(list)
        for i in range(1, self.n+1):
            par = self.find(i)
            d[par].append(i)
        return d

n,q = map(int,input().split())

uf = UnionFind(n)
p = list(map(int,input().split()))
for i in range(n):
    if p[i] == -1: continue
    uf.union(i+1, p[i])

for i in range(q):
    a,b = map(int,input().split())
    if uf.same(a,b):
        print('Yes')
    else:
        print('No')
0