結果
問題 | No.2202 贅沢てりたまチキン |
ユーザー | ohana |
提出日時 | 2023-10-23 14:27:37 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 678 ms / 2,000 ms |
コード長 | 2,016 bytes |
コンパイル時間 | 298 ms |
コンパイル使用メモリ | 81,968 KB |
実行使用メモリ | 110,048 KB |
最終ジャッジ日時 | 2024-09-22 10:06:54 |
合計ジャッジ時間 | 6,875 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 42 ms
55,504 KB |
testcase_01 | AC | 42 ms
55,700 KB |
testcase_02 | AC | 41 ms
54,436 KB |
testcase_03 | AC | 41 ms
55,132 KB |
testcase_04 | AC | 41 ms
55,464 KB |
testcase_05 | AC | 41 ms
54,860 KB |
testcase_06 | AC | 41 ms
55,632 KB |
testcase_07 | AC | 41 ms
56,120 KB |
testcase_08 | AC | 41 ms
54,260 KB |
testcase_09 | AC | 42 ms
55,928 KB |
testcase_10 | AC | 114 ms
100,924 KB |
testcase_11 | AC | 42 ms
55,556 KB |
testcase_12 | AC | 42 ms
55,792 KB |
testcase_13 | AC | 42 ms
54,316 KB |
testcase_14 | AC | 237 ms
101,428 KB |
testcase_15 | AC | 244 ms
102,544 KB |
testcase_16 | AC | 240 ms
108,576 KB |
testcase_17 | AC | 237 ms
108,828 KB |
testcase_18 | AC | 162 ms
93,372 KB |
testcase_19 | AC | 219 ms
108,628 KB |
testcase_20 | AC | 278 ms
110,048 KB |
testcase_21 | AC | 277 ms
109,976 KB |
testcase_22 | AC | 186 ms
87,944 KB |
testcase_23 | AC | 307 ms
88,804 KB |
testcase_24 | AC | 256 ms
87,680 KB |
testcase_25 | AC | 678 ms
105,824 KB |
testcase_26 | AC | 255 ms
102,648 KB |
testcase_27 | AC | 257 ms
102,712 KB |
ソースコード
from collections import deque from collections import defaultdict 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, m = map(int, input().split()) A = [[] for i in range(n)] B = [None for i in range(n)] uf = UnionFind(n) for i in range(m): a, b = map(lambda x: int(x) - 1, input().split()) A[a].append(b) A[b].append(a) uf.union(a, b) ok = set() for i in range(n): if B[i] is None and not uf.find(i) in ok: B[i] = True dq = deque([i]) while dq: q = dq.popleft() for a in A[q]: if B[a] is None: B[a] = not B[q] dq.append(a) elif B[a] == B[q]: ok.add(uf.find(i)) break print("Yes" if uf.group_count() == len(ok) else "No")