結果
問題 | No.2403 "Eight" Bridges of Königsberg |
ユーザー | mattu34 |
提出日時 | 2023-09-08 01:56:51 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 269 ms / 2,000 ms |
コード長 | 1,946 bytes |
コンパイル時間 | 284 ms |
コンパイル使用メモリ | 82,048 KB |
実行使用メモリ | 110,848 KB |
最終ジャッジ日時 | 2024-06-25 19:02:08 |
合計ジャッジ時間 | 5,851 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 45 ms
55,040 KB |
testcase_01 | AC | 45 ms
54,784 KB |
testcase_02 | AC | 45 ms
54,784 KB |
testcase_03 | AC | 45 ms
54,528 KB |
testcase_04 | AC | 187 ms
77,692 KB |
testcase_05 | AC | 239 ms
77,772 KB |
testcase_06 | AC | 258 ms
78,484 KB |
testcase_07 | AC | 188 ms
77,696 KB |
testcase_08 | AC | 216 ms
78,176 KB |
testcase_09 | AC | 269 ms
78,492 KB |
testcase_10 | AC | 234 ms
77,696 KB |
testcase_11 | AC | 231 ms
77,924 KB |
testcase_12 | AC | 257 ms
77,656 KB |
testcase_13 | AC | 235 ms
78,096 KB |
testcase_14 | AC | 45 ms
55,168 KB |
testcase_15 | AC | 46 ms
55,040 KB |
testcase_16 | AC | 45 ms
54,784 KB |
testcase_17 | AC | 44 ms
55,040 KB |
testcase_18 | AC | 46 ms
54,644 KB |
testcase_19 | AC | 110 ms
110,848 KB |
testcase_20 | AC | 63 ms
72,192 KB |
testcase_21 | AC | 76 ms
87,552 KB |
testcase_22 | AC | 110 ms
110,720 KB |
testcase_23 | AC | 104 ms
110,592 KB |
testcase_24 | AC | 109 ms
102,756 KB |
testcase_25 | AC | 77 ms
93,568 KB |
testcase_26 | AC | 50 ms
56,192 KB |
testcase_27 | AC | 131 ms
104,064 KB |
testcase_28 | AC | 123 ms
98,688 KB |
testcase_29 | AC | 102 ms
80,512 KB |
testcase_30 | AC | 100 ms
94,704 KB |
testcase_31 | AC | 129 ms
104,940 KB |
testcase_32 | AC | 100 ms
77,440 KB |
testcase_33 | AC | 97 ms
89,856 KB |
testcase_34 | AC | 103 ms
79,104 KB |
ソースコード
from collections import * import heapq import bisect import itertools 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()) INF = float("inf") MOD = 998244353 mod = 998244353 N, M = map(int, input().split()) indeg = [0] * N outdeg = [0] * N uf = UnionFind(N) for _ in range(M): u, v = map(int, input().split()) u -= 1 v -= 1 uf.union(u, v) indeg[v] += 1 outdeg[u] += 1 group = uf.all_group_members() # print(group) ans = 0 for p, g in group.items(): cnt = 0 ok = False for member in g: cnt += abs(indeg[member] - outdeg[member]) if outdeg[member] > 0: ok = True if not ok: continue cnt //= 2 ans += max(cnt - 1, 0) + 1 print(ans - 1)