結果
問題 | No.2316 Freight Train |
ユーザー | 倉 |
提出日時 | 2023-05-26 22:01:04 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 570 ms / 2,000 ms |
コード長 | 1,753 bytes |
コンパイル時間 | 456 ms |
コンパイル使用メモリ | 87,064 KB |
実行使用メモリ | 109,928 KB |
最終ジャッジ日時 | 2023-08-26 11:33:30 |
合計ジャッジ時間 | 14,598 ms |
ジャッジサーバーID (参考情報) |
judge11 / judge13 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 91 ms
71,576 KB |
testcase_01 | AC | 89 ms
71,348 KB |
testcase_02 | AC | 95 ms
71,552 KB |
testcase_03 | AC | 553 ms
109,524 KB |
testcase_04 | AC | 412 ms
91,920 KB |
testcase_05 | AC | 379 ms
92,392 KB |
testcase_06 | AC | 237 ms
82,536 KB |
testcase_07 | AC | 411 ms
90,056 KB |
testcase_08 | AC | 465 ms
109,732 KB |
testcase_09 | AC | 461 ms
97,068 KB |
testcase_10 | AC | 461 ms
92,016 KB |
testcase_11 | AC | 484 ms
106,280 KB |
testcase_12 | AC | 499 ms
102,464 KB |
testcase_13 | AC | 549 ms
109,744 KB |
testcase_14 | AC | 552 ms
109,632 KB |
testcase_15 | AC | 539 ms
109,860 KB |
testcase_16 | AC | 570 ms
109,628 KB |
testcase_17 | AC | 546 ms
109,620 KB |
testcase_18 | AC | 554 ms
109,748 KB |
testcase_19 | AC | 540 ms
109,628 KB |
testcase_20 | AC | 538 ms
109,928 KB |
testcase_21 | AC | 545 ms
109,872 KB |
testcase_22 | AC | 537 ms
109,728 KB |
testcase_23 | AC | 266 ms
109,476 KB |
testcase_24 | AC | 284 ms
109,428 KB |
testcase_25 | AC | 279 ms
108,992 KB |
testcase_26 | AC | 262 ms
109,160 KB |
testcase_27 | AC | 224 ms
84,336 KB |
testcase_28 | AC | 90 ms
71,348 KB |
ソースコード
from collections import defaultdict # @nkmk / PythonでのUnion-Find(素集合データ構造)の実装と使い方 # https://note.nkmk.me/python-union-find/ class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * n def find(self, 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 = 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): 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())) uf = UnionFind(N) for i, p in enumerate(P): p -= 1 if p < 0: continue uf.union(i, p) ans = [] for _ in range(Q): a, b = map(int, input().split()) a, b = a-1, b-1 ans.append("Yes" if uf.same(a, b) else "No") for a in ans: print(a)