結果
問題 |
No.2403 "Eight" Bridges of Königsberg
|
ユーザー |
|
提出日時 | 2023-08-04 22:26:49 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,375 bytes |
コンパイル時間 | 185 ms |
コンパイル使用メモリ | 82,196 KB |
実行使用メモリ | 81,556 KB |
最終ジャッジ日時 | 2024-10-14 20:26:36 |
合計ジャッジ時間 | 5,076 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 25 WA * 6 |
ソースコード
class UnionFind: def __init__(self, n): self.n = n self.par = [-1] * n self.group_ = n def find(self, x): if self.par[x] < 0: return x lst = [] while self.par[x] >= 0: lst.append(x) x = self.par[x] for y in lst: self.par[y] = x return x def unite(self, x, y): x = self.find(x) y = self.find(y) if x == y: return False if self.par[x] > self.par[y]: x, y = y, x self.par[x] += self.par[y] self.par[y] = x self.group_ -= 1 return True def size(self, x): return -self.par[self.find(x)] def same(self, x, y): return self.find(x) == self.find(y) @property def group(self): return self.group_ n, m = map(int, input().split()) in_ = [0] * n out_ = [0] * n UF = UnionFind(n) for _ in range(m): u, v = map(int, input().split()) u -= 1 v -= 1 in_[v] += 1 out_[u] += 1 UF.unite(u, v) tot = 0 for i, o in zip(in_, out_): tot += max(0, i - o) C = [False] * n for i in range(n): if in_[i] != out_[i]: C[UF.find(i)] = True z = 0 for i in range(n): if UF.find(i) == i and not C[i] and UF.size(i) > 1: z += 1 C[i] = True print(max(0, tot - 1) + min(z, UF.group - 1))