結果

問題 No.2418 情報通だよ!Nafmoくん
ユーザー lloyzlloyz
提出日時 2023-08-22 00:04:09
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 349 ms / 2,000 ms
コード長 2,294 bytes
コンパイル時間 278 ms
コンパイル使用メモリ 87,268 KB
実行使用メモリ 82,496 KB
最終ジャッジ日時 2023-08-22 00:04:18
合計ジャッジ時間 7,028 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 93 ms
71,324 KB
testcase_01 AC 91 ms
71,380 KB
testcase_02 AC 90 ms
71,380 KB
testcase_03 AC 263 ms
82,440 KB
testcase_04 AC 145 ms
80,544 KB
testcase_05 AC 305 ms
81,784 KB
testcase_06 AC 215 ms
81,748 KB
testcase_07 AC 290 ms
79,908 KB
testcase_08 AC 212 ms
81,872 KB
testcase_09 AC 349 ms
81,568 KB
testcase_10 AC 274 ms
82,284 KB
testcase_11 AC 258 ms
82,092 KB
testcase_12 AC 228 ms
80,640 KB
testcase_13 AC 246 ms
80,244 KB
testcase_14 AC 234 ms
80,536 KB
testcase_15 AC 173 ms
78,044 KB
testcase_16 AC 343 ms
82,496 KB
testcase_17 AC 192 ms
79,680 KB
testcase_18 AC 234 ms
80,816 KB
testcase_19 AC 133 ms
79,844 KB
testcase_20 AC 179 ms
78,052 KB
testcase_21 AC 214 ms
80,216 KB
testcase_22 AC 242 ms
79,752 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict

class UnionFind():
    def __init__(self, n):
        '''
        UnionFindクラス。nは要素数を表す。
        '''
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        '''
        要素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を含む集合と要素yを含む集合を合体する関数。
        基本的には、要素数が多い集合に統合される。
        要素数が同じときは要素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):
        '''
        要素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 all_group_members(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 '\n'.join(f'{r}: {m}' for r, m in self.all_group_members().items())

n, m = map(int, input().split())
UF = UnionFind(2 * n)
G = [1 for _ in range(2 * n)]
for _ in range(m):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    if UF.same(a, b):
        continue
    a_root = UF.find(a)
    b_root = UF.find(b)
    UF.union(a, b)
    root = UF.find(a)
    if root == a_root:
        G[a_root] += G[b_root]
        G[b_root] = 0
    else:
        G[b_root] += G[a_root]
        G[a_root] = 0
res = 0
for i in range(2 * n):
    res += G[i] % 2
print(res // 2)
0