結果
問題 |
No.2403 "Eight" Bridges of Königsberg
|
ユーザー |
![]() |
提出日時 | 2023-08-05 12:18:03 |
言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,484 bytes |
コンパイル時間 | 83 ms |
コンパイル使用メモリ | 12,928 KB |
実行使用メモリ | 48,256 KB |
最終ジャッジ日時 | 2024-10-15 09:22:33 |
合計ジャッジ時間 | 6,223 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 15 WA * 16 |
ソースコード
import sys input = sys.stdin.readline def main(): N, M = map(int, input().split()) l = [list(map(int, input().split())) for l in range(M)] 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 roots(self): return [i for i, x in enumerate(self.parents) if x < 0] def group_count(self): return len(self.roots()) lout = [0] * N lin = [0] * N lans = [0] * N uf = UnionFind(N) for i in range(M): uf.union(l[i][0] - 1, l[i][1] - 1) lout[l[i][0] - 1] += 1 lin[l[i][1] - 1] += 1 ecpt = 0 for i in range(N): lans[i] = lout[i] - lin[i] if lout[i] == lin[i] == 0: ecpt += 1 rc = uf.group_count() if set(lans) == {0}: print(rc - 1 - ecpt) else: print(sum([i for i in lans if i > 0]) - 1 + (rc - 1) - ecpt) if __name__ == '__main__': main()