結果
問題 | No.2403 "Eight" Bridges of Königsberg |
ユーザー | mattu34 |
提出日時 | 2023-09-08 01:51:26 |
言語 | PyPy3 (7.3.15) |
結果 |
TLE
|
実行時間 | - |
コード長 | 1,895 bytes |
コンパイル時間 | 535 ms |
コンパイル使用メモリ | 82,292 KB |
実行使用メモリ | 98,944 KB |
最終ジャッジ日時 | 2024-06-25 18:56:48 |
合計ジャッジ時間 | 8,019 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 49 ms
59,904 KB |
testcase_01 | AC | 53 ms
54,784 KB |
testcase_02 | AC | 48 ms
54,808 KB |
testcase_03 | AC | 49 ms
54,736 KB |
testcase_04 | AC | 188 ms
77,704 KB |
testcase_05 | AC | 259 ms
77,860 KB |
testcase_06 | AC | 275 ms
78,612 KB |
testcase_07 | AC | 199 ms
77,696 KB |
testcase_08 | AC | 229 ms
77,696 KB |
testcase_09 | AC | 286 ms
78,112 KB |
testcase_10 | AC | 238 ms
77,460 KB |
testcase_11 | AC | 238 ms
78,048 KB |
testcase_12 | AC | 274 ms
77,860 KB |
testcase_13 | AC | 247 ms
77,836 KB |
testcase_14 | AC | 51 ms
54,912 KB |
testcase_15 | AC | 52 ms
54,912 KB |
testcase_16 | AC | 51 ms
54,912 KB |
testcase_17 | AC | 50 ms
54,528 KB |
testcase_18 | AC | 51 ms
55,040 KB |
testcase_19 | TLE | - |
testcase_20 | -- | - |
testcase_21 | -- | - |
testcase_22 | -- | - |
testcase_23 | -- | - |
testcase_24 | -- | - |
testcase_25 | -- | - |
testcase_26 | -- | - |
testcase_27 | -- | - |
testcase_28 | -- | - |
testcase_29 | -- | - |
testcase_30 | -- | - |
testcase_31 | -- | - |
testcase_32 | -- | - |
testcase_33 | -- | - |
testcase_34 | -- | - |
ソースコード
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.roots() ans = 0 for g in group: cnt = 0 ok = False for member in uf.members(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)