結果
問題 | No.2316 Freight Train |
ユーザー | lloyz |
提出日時 | 2023-05-26 21:30:43 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 719 ms / 2,000 ms |
コード長 | 2,326 bytes |
コンパイル時間 | 421 ms |
コンパイル使用メモリ | 87,016 KB |
実行使用メモリ | 127,752 KB |
最終ジャッジ日時 | 2023-08-26 10:25:33 |
合計ジャッジ時間 | 18,052 ms |
ジャッジサーバーID (参考情報) |
judge11 / judge12 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 89 ms
71,680 KB |
testcase_01 | AC | 82 ms
71,348 KB |
testcase_02 | AC | 83 ms
71,692 KB |
testcase_03 | AC | 693 ms
127,752 KB |
testcase_04 | AC | 455 ms
116,648 KB |
testcase_05 | AC | 367 ms
116,728 KB |
testcase_06 | AC | 197 ms
81,488 KB |
testcase_07 | AC | 556 ms
94,036 KB |
testcase_08 | AC | 501 ms
127,176 KB |
testcase_09 | AC | 548 ms
123,408 KB |
testcase_10 | AC | 579 ms
117,548 KB |
testcase_11 | AC | 543 ms
125,828 KB |
testcase_12 | AC | 598 ms
125,472 KB |
testcase_13 | AC | 689 ms
127,428 KB |
testcase_14 | AC | 698 ms
127,188 KB |
testcase_15 | AC | 705 ms
126,656 KB |
testcase_16 | AC | 698 ms
126,852 KB |
testcase_17 | AC | 668 ms
127,188 KB |
testcase_18 | AC | 699 ms
127,088 KB |
testcase_19 | AC | 705 ms
127,076 KB |
testcase_20 | AC | 719 ms
127,112 KB |
testcase_21 | AC | 710 ms
126,716 KB |
testcase_22 | AC | 687 ms
127,156 KB |
testcase_23 | AC | 575 ms
127,160 KB |
testcase_24 | AC | 600 ms
127,152 KB |
testcase_25 | AC | 625 ms
122,164 KB |
testcase_26 | AC | 583 ms
122,084 KB |
testcase_27 | AC | 478 ms
77,988 KB |
testcase_28 | AC | 83 ms
71,656 KB |
ソースコード
from collections import defaultdict, deque class UnionFind(): def __init__(self, n): ''' UnionFindクラス。nは要素数を表す。 ''' self.n = n self.parents = [-1] * n def find(self, x): ''' 要素xを含む集合の親を見つける関数。 ''' if self.parents[x] < 0: return x else: self.parents[x] = self.find(self.parents[x]) return self.parents[x] def union(self, x, y): ''' 要素xを含む集合と要素yを含む集合を合体する関数。 基本的には、要素数が多い集合に統合される。 要素数が同じときは要素yを含む集合に統合される。 ''' x = self.find(x) y = self.find(y) if x == y: return if self.parents[x] > self.parents[y]: x, y = y, x self.parents[x] += self.parents[y] self.parents[y] = x def size(self, x): ''' 要素xを含む集合の要素数を出す関数。 ''' return -self.parents[self.find(x)] def same(self, x, y): return self.find(x) == self.find(y) 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 all_group_members(self): group_members = defaultdict(list) for member in range(self.n): group_members[self.find(member)].append(member) return group_members def __str__(self): return '\n'.join(f'{r}: {m}' for r, m in self.all_group_members().items()) n, q = map(int, input().split()) P = list(map(int, input().split())) G = defaultdict(list) R = [] for i in range(n): if P[i] == -1: R.append(i) else: G[P[i] - 1].append(i) UF = UnionFind(n) for r in R: Que = deque([r]) while Que: cp = Que.popleft() for np in G[cp]: UF.union(r, np) Que.append(np) for _ in range(q): a, b = map(int, input().split()) a -= 1 b -= 1 if UF.same(a, b): print("Yes") else: print("No")