結果
問題 | No.2418 情報通だよ!Nafmoくん |
ユーザー |
|
提出日時 | 2023-08-12 14:04:00 |
言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
結果 |
AC
|
実行時間 | 420 ms / 2,000 ms |
コード長 | 2,688 bytes |
コンパイル時間 | 179 ms |
コンパイル使用メモリ | 12,672 KB |
実行使用メモリ | 53,752 KB |
最終ジャッジ日時 | 2024-11-14 12:23:04 |
合計ジャッジ時間 | 6,183 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 21 |
ソースコード
import types_atcoder_code = """# Python port of AtCoder Library.__version__ = '0.0.1'"""atcoder = types.ModuleType('atcoder')exec(_atcoder_code, atcoder.__dict__)_atcoder_dsu_code = """import typingclass DSU:'''Implement (union by size) + (path halving)Reference:Zvi Galil and Giuseppe F. Italiano,Data structures and algorithms for disjoint set union problems'''def __init__(self, n: int = 0) -> None:self._n = nself.parent_or_size = [-1] * ndef merge(self, a: int, b: int) -> int:assert 0 <= a < self._nassert 0 <= b < self._nx = self.leader(a)y = self.leader(b)if x == y:return xif -self.parent_or_size[x] < -self.parent_or_size[y]:x, y = y, xself.parent_or_size[x] += self.parent_or_size[y]self.parent_or_size[y] = xreturn xdef same(self, a: int, b: int) -> bool:assert 0 <= a < self._nassert 0 <= b < self._nreturn self.leader(a) == self.leader(b)def leader(self, a: int) -> int:assert 0 <= a < self._nparent = self.parent_or_size[a]while parent >= 0:if self.parent_or_size[parent] < 0:return parentself.parent_or_size[a], a, parent = (self.parent_or_size[parent],self.parent_or_size[parent],self.parent_or_size[self.parent_or_size[parent]])return adef size(self, a: int) -> int:assert 0 <= a < self._nreturn -self.parent_or_size[self.leader(a)]def groups(self) -> typing.List[typing.List[int]]:leader_buf = [self.leader(i) for i in range(self._n)]result: typing.List[typing.List[int]] = [[] for _ in range(self._n)]for i in range(self._n):result[leader_buf[i]].append(i)return list(filter(lambda r: r, result))"""atcoder.dsu = types.ModuleType('atcoder.dsu')exec(_atcoder_dsu_code, atcoder.dsu.__dict__)DSU = atcoder.dsu.DSUimport sysinput = sys.stdin.readline# from atcoder.dsu import DSUdef read():N, M = map(int, input().strip().split())AB = []for i in range(M):a, b = map(int, input().strip().split())AB.append((a, b))return N, M, ABdef solve(N, M, AB):dsu = DSU(2 * N)for a, b in AB:dsu.merge(a-1, b-1)ans = 0for group in dsu.groups():ans += len(group) % 2ans //= 2return ansif __name__ == "__main__":inputs = read()outputs = solve(*inputs)if outputs is not None:print("%s" % str(outputs))