結果

問題 No.2418 情報通だよ!Nafmoくん
ユーザー SuzuxSuzux
提出日時 2023-08-12 15:29:53
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 285 ms / 2,000 ms
コード長 3,575 bytes
コンパイル時間 334 ms
コンパイル使用メモリ 12,928 KB
実行使用メモリ 22,784 KB
最終ジャッジ日時 2024-04-30 09:35:54
合計ジャッジ時間 5,414 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 28 ms
10,880 KB
testcase_01 AC 27 ms
10,880 KB
testcase_02 AC 27 ms
10,880 KB
testcase_03 AC 285 ms
21,120 KB
testcase_04 AC 111 ms
19,968 KB
testcase_05 AC 181 ms
13,824 KB
testcase_06 AC 203 ms
20,736 KB
testcase_07 AC 187 ms
12,928 KB
testcase_08 AC 209 ms
22,784 KB
testcase_09 AC 225 ms
14,720 KB
testcase_10 AC 268 ms
19,840 KB
testcase_11 AC 244 ms
19,456 KB
testcase_12 AC 178 ms
18,176 KB
testcase_13 AC 94 ms
13,568 KB
testcase_14 AC 154 ms
15,872 KB
testcase_15 AC 174 ms
11,136 KB
testcase_16 AC 267 ms
16,768 KB
testcase_17 AC 99 ms
11,264 KB
testcase_18 AC 173 ms
19,200 KB
testcase_19 AC 97 ms
19,200 KB
testcase_20 AC 189 ms
11,136 KB
testcase_21 AC 112 ms
14,592 KB
testcase_22 AC 113 ms
11,648 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import math
from sys import stdin

from collections import defaultdict

class UnionFind():
    """
    Union Find木クラス

    Attributes
    --------------------
    n : int
        要素数
    root : list
        木の要素数
        0未満であればそのノードが根であり、添字の値が要素数
    rank : list
        木の深さ
    """

    def __init__(self, n):
        """
        Parameters
        ---------------------
        n : int
            要素数
        """
        self.n = n
        self.root = [-1]*(n+1)
        self.rank = [0]*(n+1)

    def find(self, x):
        """
        ノードxの根を見つける

        Parameters
        ---------------------
        x : int
            見つけるノード

        Returns
        ---------------------
        root : int
            根のノード
        """
        if(self.root[x] < 0):
            return x
        else:
            self.root[x] = self.find(self.root[x])
            return self.root[x]

    def unite(self, x, y):
        """
        木の併合

        Parameters
        ---------------------
        x : int
            併合したノード
        y : int
            併合したノード
        """
        x = self.find(x)
        y = self.find(y)

        if(x == y):
            return
        elif(self.rank[x] > self.rank[y]):
            self.root[x] += self.root[y]
            self.root[y] = x
        else:
            self.root[y] += self.root[x]
            self.root[x] = y
            if(self.rank[x] == self.rank[y]):
                self.rank[y] += 1

    def same(self, x, y):
        """
        同じグループに属するか判定

        Parameters
        ---------------------
        x : int
            判定したノード
        y : int
            判定したノード

        Returns
        ---------------------
        ans : bool
            同じグループに属しているか
        """
        return self.find(x) == self.find(y)

    def size(self, x):
        """
        木のサイズを計算

        Parameters
        ---------------------
        x : int
            計算したい木のノード

        Returns
        ---------------------
        size : int
            木のサイズ
        """
        return -self.root[self.find(x)]

    def roots(self):
        """
        根のノードを取得

        Returns
        ---------------------
        roots : list
            根のノード
        """
        return [i for i, x in enumerate(self.root) if x < 0]

    def group_size(self):
        """
        グループ数を取得

        Returns
        ---------------------
        size : int
            グループ数
        """
        return len(self.roots())

    def group_members(self):
        """
        全てのグループごとのノードを取得

        Returns
        ---------------------
        group_members : defaultdict
            根をキーとしたノードのリスト
        """
        group_members = defaultdict(list)
        for member in range(self.n):
            group_members[self.find(member)].append(member)
        return group_members


# ---------------------------------------- #

n, m = list(map(int, input().split()))
searched = [False] * (2*n)
uf = UnionFind(2*n - 1)

for _ in range(m):
    a, b = list(map(int, stdin.readline().split()))
    uf.unite(a-1, b-1)

ans = 0
temp = 0
for r in uf.roots():
    uSize = uf.size(r)
    if uSize % 2 == 1:
        temp += 1
ans = math.ceil(temp/2)
print(ans)
0