結果
問題 | No.2418 情報通だよ!Nafmoくん |
ユーザー |
|
提出日時 | 2023-08-12 13:53:06 |
言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
結果 |
AC
|
実行時間 | 499 ms / 2,000 ms |
コード長 | 1,086 bytes |
コンパイル時間 | 244 ms |
コンパイル使用メモリ | 12,672 KB |
実行使用メモリ | 26,240 KB |
最終ジャッジ日時 | 2024-11-14 12:22:23 |
合計ジャッジ時間 | 7,522 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 21 |
ソースコード
class UnionFind: def __init__( self, n: int, ) -> None: self.forest = [-1] * n def union(self, x: int, y: int) -> None: x = self.findRoot(x) y = self.findRoot(y) if x == y: return if self.forest[x] > self.forest[y]: x, y = y, x self.forest[x] += self.forest[y] self.forest[y] = x def findRoot(self, x: int) -> int: if self.forest[x] < 0: return x else: self.forest[x] = self.findRoot(self.forest[x]) return self.forest[x] def issame(self, x: int, y: int) -> bool: return self.findRoot(x) == self.findRoot(y) def size(self, x: int) -> int: return -self.forest[self.findRoot(x)] n, m = map(int, input().split()) ab = [tuple(map(int, input().split())) for _ in range(m)] uf = UnionFind(n * 2) for a, b in ab: uf.union(a - 1, b - 1) groups = [] for i in range(n * 2): if i == uf.findRoot(i): groups.append(uf.size(i)) ans = 0 for g in groups: ans += g % 2 print(ans // 2)