結果

問題 No.2316 Freight Train
ユーザー sepa38
提出日時 2023-05-26 21:24:23
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 790 bytes
コンパイル時間 199 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 32,024 KB
最終ジャッジ日時 2024-12-25 04:36:13
合計ジャッジ時間 50,831 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 14 TLE * 12
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(500000)
class UF:
  def __init__(self, n):
    self.root = [i for i in range(n)]
    self.subt = [1] * n

  def find(self, x):
    if self.root[x] == x:
      return x
    else:
      self.root[x] = self.find(self.root[x])
      return self.root[x]

  def union(self, x, y):
    x, y = self.find(x), self.find(y)
    x, y = min(x, y), max(x, y)
    if x != y:
      self.subt[x] += self.subt[y]
    self.root[y] = x

  def size(self, x):
    return self.subt[self.find(x)]

n, q = map(int, input().split())
p = list(map(lambda x: int(x)-1, input().split()))
uf = UF(n)
for i in range(n):
  if p[i] < 0:
    continue
  uf.union(i, p[i])
for _ in range(q):
  a, b = map(lambda x: int(x)-1, input().split())
  print("Yes" if uf.find(a) == uf.find(b) else "No")
0