結果
問題 | No.2403 "Eight" Bridges of Königsberg |
ユーザー | lloyz |
提出日時 | 2023-08-05 14:37:48 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,489 bytes |
コンパイル時間 | 523 ms |
コンパイル使用メモリ | 82,048 KB |
実行使用メモリ | 120,832 KB |
最終ジャッジ日時 | 2024-04-23 11:45:05 |
合計ジャッジ時間 | 5,676 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 39 ms
54,144 KB |
testcase_01 | AC | 40 ms
53,888 KB |
testcase_02 | AC | 39 ms
53,888 KB |
testcase_03 | AC | 50 ms
54,144 KB |
testcase_04 | AC | 178 ms
78,328 KB |
testcase_05 | AC | 254 ms
79,512 KB |
testcase_06 | AC | 268 ms
79,016 KB |
testcase_07 | AC | 195 ms
78,464 KB |
testcase_08 | AC | 227 ms
78,720 KB |
testcase_09 | AC | 277 ms
79,360 KB |
testcase_10 | AC | 228 ms
78,316 KB |
testcase_11 | AC | 228 ms
78,920 KB |
testcase_12 | AC | 261 ms
79,020 KB |
testcase_13 | AC | 231 ms
79,024 KB |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | AC | 112 ms
111,104 KB |
testcase_20 | AC | 62 ms
71,936 KB |
testcase_21 | AC | 80 ms
87,552 KB |
testcase_22 | AC | 109 ms
110,592 KB |
testcase_23 | AC | 104 ms
110,336 KB |
testcase_24 | AC | 107 ms
100,736 KB |
testcase_25 | AC | 79 ms
93,440 KB |
testcase_26 | WA | - |
testcase_27 | WA | - |
testcase_28 | AC | 121 ms
98,560 KB |
testcase_29 | WA | - |
testcase_30 | AC | 97 ms
92,544 KB |
testcase_31 | AC | 150 ms
120,832 KB |
testcase_32 | WA | - |
testcase_33 | WA | - |
testcase_34 | WA | - |
ソースコード
from collections import defaultdict 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, m = map(int, input().split()) C = [0 for _ in range(n)] UF = UnionFind(n) G = [1 for _ in range(n)] for _ in range(m): u, v = map(int, input().split()) u -= 1 v -= 1 C[u] -= 1 C[v] += 1 if UF.same(u, v): continue uroot = UF.find(u) vroot = UF.find(v) UF.union(u, v) root = UF.find(u) if root == uroot: G[uroot] += G[vroot] G[vroot] = 0 else: G[vroot] += G[uroot] G[uroot] = 0 Gs = UF.all_group_members() ans = -1 for GG in Gs.values(): res = 0 ok = False for idx in GG: if C[idx] != 0: ok = True res += max(C[idx], 0) ans += max(res - 1, 0) + ok print(ans)