結果

問題 No.2316 Freight Train
ユーザー prin_kemkemprin_kemkem
提出日時 2023-05-27 00:33:42
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 660 ms / 2,000 ms
コード長 2,612 bytes
コンパイル時間 268 ms
コンパイル使用メモリ 86,608 KB
実行使用メモリ 114,584 KB
最終ジャッジ日時 2023-08-26 14:29:57
合計ジャッジ時間 17,327 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 158 ms
80,848 KB
testcase_01 AC 156 ms
80,988 KB
testcase_02 AC 157 ms
80,712 KB
testcase_03 AC 606 ms
114,320 KB
testcase_04 AC 480 ms
96,796 KB
testcase_05 AC 438 ms
96,696 KB
testcase_06 AC 294 ms
84,748 KB
testcase_07 AC 446 ms
88,248 KB
testcase_08 AC 549 ms
114,484 KB
testcase_09 AC 521 ms
101,448 KB
testcase_10 AC 507 ms
96,544 KB
testcase_11 AC 545 ms
110,564 KB
testcase_12 AC 551 ms
107,348 KB
testcase_13 AC 575 ms
113,920 KB
testcase_14 AC 578 ms
114,388 KB
testcase_15 AC 583 ms
114,436 KB
testcase_16 AC 585 ms
114,156 KB
testcase_17 AC 606 ms
113,980 KB
testcase_18 AC 577 ms
114,236 KB
testcase_19 AC 597 ms
114,384 KB
testcase_20 AC 654 ms
114,408 KB
testcase_21 AC 660 ms
114,380 KB
testcase_22 AC 660 ms
114,584 KB
testcase_23 AC 265 ms
114,384 KB
testcase_24 AC 283 ms
114,376 KB
testcase_25 AC 272 ms
112,820 KB
testcase_26 AC 260 ms
112,800 KB
testcase_27 AC 208 ms
82,796 KB
testcase_28 AC 157 ms
80,844 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict, deque, Counter
import copy
from itertools import combinations, permutations, product, accumulate, groupby
from heapq import heapify, heappop, heappush
import math
import bisect
from pprint import pprint
from random import randint
import sys
# sys.setrecursionlimit(700000)
input = lambda: sys.stdin.readline().rstrip('\n')
inf = float('inf')
mod1 = 10**9+7
mod2 = 998244353
def ceil_div(x, y): return -(-x//y)

#################################################

class UnionFind:
    #コンストラクタ
    def __init__(self, n):
        self.n = n
        self.parents = [-1]*n

    #点xの根を調べる+親が根になるよう移動
    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    #点x,yの属する集合同士を連結(要素数が少ない方を多い方に連結)
    #辺を追加したらTrue, しなければFalseを返す
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return False
        if self.parents[x] > self.parents[y]:
            x, y = y, x
        self.parents[x] += self.parents[y]
        self.parents[y] = x
        return True

    #点xが属する集合の要素数を取得
    def size(self, x):
        return -self.parents[self.find(x)]

    #点x,yが同じ集合に属しているか判定
    def same(self, x, y):
        return self.find(x) == self.find(y)

    #点xの属する集合の全要素を取得
    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    #根になっている全要素を取得
    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    #集合の数を取得
    def group_count(self):
        return len(self.roots())

    #全集合の「根と全要素」を取得
    def get_groups(self):
        groups = defaultdict(list)
        for x in range(self.n):
            groups[self.find(x)].append(x)
        return groups

    #print(インスタンス)で、全集合の「根と全要素」を出力
    def __str__(self):
        return '\n'.join('{}:{}'.format(r, self.menbers(r)) for r in self.roots())

N, Q = map(int, input().split())
P = list(map(lambda x: int(x)-1, input().split()))
uf = UnionFind(N)
for i, p in enumerate(P):
    if p == -2: continue
    uf.union(i, p)
for _ in range(Q):
    a, b = map(int, input().split())
    a -= 1; b -= 1
    print("Yes" if uf.same(a, b) else "No")
0