結果
問題 | No.2403 "Eight" Bridges of Königsberg |
ユーザー | Mottchan |
提出日時 | 2023-08-05 10:54:19 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 528 ms / 2,000 ms |
コード長 | 1,812 bytes |
コンパイル時間 | 212 ms |
コンパイル使用メモリ | 82,304 KB |
実行使用メモリ | 188,336 KB |
最終ジャッジ日時 | 2024-05-05 01:44:58 |
合計ジャッジ時間 | 8,937 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 43 ms
54,016 KB |
testcase_01 | AC | 45 ms
54,144 KB |
testcase_02 | AC | 41 ms
54,016 KB |
testcase_03 | AC | 42 ms
54,272 KB |
testcase_04 | AC | 269 ms
88,916 KB |
testcase_05 | AC | 437 ms
105,464 KB |
testcase_06 | AC | 501 ms
110,492 KB |
testcase_07 | AC | 298 ms
93,064 KB |
testcase_08 | AC | 366 ms
97,468 KB |
testcase_09 | AC | 528 ms
110,336 KB |
testcase_10 | AC | 430 ms
106,680 KB |
testcase_11 | AC | 382 ms
100,812 KB |
testcase_12 | AC | 465 ms
110,364 KB |
testcase_13 | AC | 409 ms
100,804 KB |
testcase_14 | AC | 47 ms
54,016 KB |
testcase_15 | AC | 47 ms
54,016 KB |
testcase_16 | AC | 47 ms
54,016 KB |
testcase_17 | AC | 46 ms
54,016 KB |
testcase_18 | AC | 47 ms
54,016 KB |
testcase_19 | AC | 247 ms
169,672 KB |
testcase_20 | AC | 109 ms
92,972 KB |
testcase_21 | AC | 181 ms
137,764 KB |
testcase_22 | AC | 228 ms
169,600 KB |
testcase_23 | AC | 222 ms
179,760 KB |
testcase_24 | AC | 211 ms
168,556 KB |
testcase_25 | AC | 191 ms
155,652 KB |
testcase_26 | AC | 56 ms
56,832 KB |
testcase_27 | AC | 308 ms
188,284 KB |
testcase_28 | AC | 284 ms
165,248 KB |
testcase_29 | AC | 146 ms
92,032 KB |
testcase_30 | AC | 171 ms
125,424 KB |
testcase_31 | AC | 288 ms
188,336 KB |
testcase_32 | AC | 115 ms
77,440 KB |
testcase_33 | AC | 147 ms
111,612 KB |
testcase_34 | AC | 134 ms
86,656 KB |
ソースコード
from collections import defaultdict 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 group(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 ''.join(f'{r}: {m}' for r, m in self.group().items()) n,m = map(int,input().split()) uf = UnionFind(n) graph = [[] for i in range(n+1)] e=[] dout = defaultdict(int) din = defaultdict(int) for i in range(m): a,b = map(int,input().split()) a -= 1 b -= 1 graph[a].append(b) e.append([a,b]) uf.union(a,b) dout[b]+=1 din[a]+=1 d=defaultdict(list) cnt=-1 for i,l in uf.group().items(): if din[i] > 0: cnt+=1 for j in l: d[i].append(abs(din[j] - dout[j])) ans=0 for k,v in d.items(): s = max(sum(v)-1,0) ans += s//2 else: print(ans+cnt)