結果

問題 No.2316 Freight Train
ユーザー sepa38sepa38
提出日時 2023-05-26 21:24:23
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,655 ms / 2,000 ms
コード長 790 bytes
コンパイル時間 89 ms
コンパイル使用メモリ 10,956 KB
実行使用メモリ 32,820 KB
最終ジャッジ日時 2023-08-26 09:58:06
合計ジャッジ時間 35,244 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 14 ms
8,232 KB
testcase_01 AC 13 ms
8,108 KB
testcase_02 AC 14 ms
8,160 KB
testcase_03 AC 1,586 ms
30,244 KB
testcase_04 AC 820 ms
20,044 KB
testcase_05 AC 601 ms
19,292 KB
testcase_06 AC 190 ms
10,052 KB
testcase_07 AC 1,216 ms
14,368 KB
testcase_08 AC 966 ms
32,732 KB
testcase_09 AC 1,118 ms
24,064 KB
testcase_10 AC 1,203 ms
20,408 KB
testcase_11 AC 1,126 ms
30,360 KB
testcase_12 AC 1,339 ms
28,296 KB
testcase_13 AC 1,610 ms
32,620 KB
testcase_14 AC 1,602 ms
32,736 KB
testcase_15 AC 1,595 ms
32,756 KB
testcase_16 AC 1,655 ms
32,668 KB
testcase_17 AC 1,589 ms
32,676 KB
testcase_18 AC 1,588 ms
32,820 KB
testcase_19 AC 1,647 ms
32,672 KB
testcase_20 AC 1,608 ms
32,804 KB
testcase_21 AC 1,621 ms
32,644 KB
testcase_22 AC 1,585 ms
32,680 KB
testcase_23 AC 1,386 ms
32,712 KB
testcase_24 AC 1,442 ms
32,756 KB
testcase_25 AC 1,288 ms
25,016 KB
testcase_26 AC 1,236 ms
25,056 KB
testcase_27 AC 1,068 ms
8,276 KB
testcase_28 AC 14 ms
8,168 KB
権限があれば一括ダウンロードができます

ソースコード

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