結果
問題 | No.2403 "Eight" Bridges of Königsberg |
ユーザー | miya145592 |
提出日時 | 2023-08-05 01:54:06 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,718 bytes |
コンパイル時間 | 183 ms |
コンパイル使用メモリ | 82,132 KB |
実行使用メモリ | 98,824 KB |
最終ジャッジ日時 | 2024-10-14 23:45:23 |
合計ジャッジ時間 | 6,326 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 42 ms
54,424 KB |
testcase_01 | AC | 43 ms
54,112 KB |
testcase_02 | AC | 43 ms
54,184 KB |
testcase_03 | AC | 43 ms
54,692 KB |
testcase_04 | AC | 231 ms
87,136 KB |
testcase_05 | AC | 329 ms
96,044 KB |
testcase_06 | AC | 377 ms
97,936 KB |
testcase_07 | AC | 254 ms
89,616 KB |
testcase_08 | AC | 298 ms
92,284 KB |
testcase_09 | AC | 375 ms
98,824 KB |
testcase_10 | AC | 332 ms
98,384 KB |
testcase_11 | AC | 301 ms
93,044 KB |
testcase_12 | AC | 345 ms
98,488 KB |
testcase_13 | AC | 308 ms
92,088 KB |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | AC | 60 ms
72,764 KB |
testcase_20 | AC | 54 ms
64,708 KB |
testcase_21 | AC | 56 ms
67,476 KB |
testcase_22 | AC | 59 ms
72,080 KB |
testcase_23 | AC | 56 ms
69,804 KB |
testcase_24 | AC | 63 ms
72,364 KB |
testcase_25 | AC | 56 ms
67,668 KB |
testcase_26 | WA | - |
testcase_27 | WA | - |
testcase_28 | AC | 101 ms
84,072 KB |
testcase_29 | WA | - |
testcase_30 | AC | 64 ms
70,236 KB |
testcase_31 | AC | 102 ms
85,852 KB |
testcase_32 | WA | - |
testcase_33 | WA | - |
testcase_34 | WA | - |
ソースコード
class UnionFind: def __init__(self, n, w=None): self.par = [-1]*n self.rank = [0]*n self.siz = [1]*n self.cnt = n self.min_node = [i for i in range(n)] self.weight = w if w else [1]*n def root(self, x): if self.par[x] == -1: return x self.par[x] = self.root(self.par[x]) return self.par[x] def issame(self, x, y): return self.root(x) == self.root(y) def unite(self, x, y): px = self.root(x) py = self.root(y) if px == py: return False if self.rank[px] < self.rank[py]: px, py = py, px self.par[py] = px if self.rank[px] == self.rank[py]: self.rank[px] += 1 self.siz[px] += self.siz[py] self.cnt -= 1 self.min_node[px] = min(self.min_node[px], self.min_node[py]) self.weight[px] += self.weight[py] return False def count(self): return self.cnt def min(self, x): return self.min_node[self.root(x)] def getweight(self, x): return self.weight[self.root(x)] def size(self, x): return self.siz[self.root(x)] from collections import defaultdict N, M = map(int, input().split()) UV = [list(map(int, input().split())) for _ in range(M)] deg = [0 for _ in range(N)] indeg = [0 for _ in range(N)] UF = UnionFind(N) for u, v in UV: u-=1 v-=1 deg[u] += 1 indeg[v] += 1 UF.unite(u, v) plus = defaultdict(int) for i in range(N): sa = indeg[i]-deg[i] r = UF.root(i) if sa>0: plus[r] += sa ans = 0 for k in plus: if plus[k]>=2: ans+=plus[k]-1 ans += len(plus)-1 print(ans)