結果

問題 No.2418 情報通だよ!Nafmoくん
ユーザー Kyoro IDKyoro ID
提出日時 2023-08-12 14:04:00
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 508 ms / 2,000 ms
コード長 2,688 bytes
コンパイル時間 393 ms
コンパイル使用メモリ 12,544 KB
実行使用メモリ 53,736 KB
最終ジャッジ日時 2024-04-30 04:52:52
合計ジャッジ時間 6,793 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
11,520 KB
testcase_01 AC 44 ms
11,520 KB
testcase_02 AC 41 ms
11,520 KB
testcase_03 AC 508 ms
53,492 KB
testcase_04 AC 195 ms
39,424 KB
testcase_05 AC 236 ms
30,692 KB
testcase_06 AC 331 ms
47,996 KB
testcase_07 AC 225 ms
29,492 KB
testcase_08 AC 366 ms
53,736 KB
testcase_09 AC 302 ms
36,184 KB
testcase_10 AC 406 ms
50,272 KB
testcase_11 AC 415 ms
46,920 KB
testcase_12 AC 303 ms
39,208 KB
testcase_13 AC 140 ms
22,624 KB
testcase_14 AC 247 ms
33,012 KB
testcase_15 AC 175 ms
22,120 KB
testcase_16 AC 370 ms
43,468 KB
testcase_17 AC 107 ms
17,792 KB
testcase_18 AC 275 ms
41,540 KB
testcase_19 AC 154 ms
36,580 KB
testcase_20 AC 189 ms
23,296 KB
testcase_21 AC 160 ms
26,064 KB
testcase_22 AC 130 ms
20,440 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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 typing


class 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 = n
        self.parent_or_size = [-1] * n

    def merge(self, a: int, b: int) -> int:
        assert 0 <= a < self._n
        assert 0 <= b < self._n

        x = self.leader(a)
        y = self.leader(b)

        if x == y:
            return x

        if -self.parent_or_size[x] < -self.parent_or_size[y]:
            x, y = y, x

        self.parent_or_size[x] += self.parent_or_size[y]
        self.parent_or_size[y] = x

        return x

    def same(self, a: int, b: int) -> bool:
        assert 0 <= a < self._n
        assert 0 <= b < self._n

        return self.leader(a) == self.leader(b)

    def leader(self, a: int) -> int:
        assert 0 <= a < self._n

        parent = self.parent_or_size[a]
        while parent >= 0:
            if self.parent_or_size[parent] < 0:
                return parent
            self.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 a

    def size(self, a: int) -> int:
        assert 0 <= a < self._n

        return -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.DSU

import sys
input = sys.stdin.readline
# from atcoder.dsu import DSU
 
 
def 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, AB
 
 
def solve(N, M, AB):
    dsu = DSU(2 * N)
    for a, b in AB:
        dsu.merge(a-1, b-1)
    ans = 0
    for group in dsu.groups():
        ans += len(group) % 2
    ans //= 2
    return ans

 
if __name__ == "__main__":
    inputs = read()
    outputs = solve(*inputs)
    if outputs is not None:
        print("%s" % str(outputs))
0