結果

問題 No.2418 情報通だよ!Nafmoくん
ユーザー FromBooskaFromBooska
提出日時 2023-08-18 19:58:49
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 252 ms / 2,000 ms
コード長 1,561 bytes
コンパイル時間 215 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 102,876 KB
最終ジャッジ日時 2024-05-06 01:43:48
合計ジャッジ時間 5,551 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
53,888 KB
testcase_01 AC 43 ms
53,888 KB
testcase_02 AC 42 ms
53,760 KB
testcase_03 AC 215 ms
93,096 KB
testcase_04 AC 122 ms
98,048 KB
testcase_05 AC 214 ms
81,664 KB
testcase_06 AC 179 ms
94,672 KB
testcase_07 AC 197 ms
80,320 KB
testcase_08 AC 186 ms
102,876 KB
testcase_09 AC 232 ms
83,688 KB
testcase_10 AC 239 ms
99,868 KB
testcase_11 AC 204 ms
90,484 KB
testcase_12 AC 170 ms
89,728 KB
testcase_13 AC 149 ms
79,556 KB
testcase_14 AC 172 ms
85,120 KB
testcase_15 AC 116 ms
76,160 KB
testcase_16 AC 252 ms
87,288 KB
testcase_17 AC 124 ms
76,868 KB
testcase_18 AC 174 ms
92,928 KB
testcase_19 AC 108 ms
96,168 KB
testcase_20 AC 128 ms
76,388 KB
testcase_21 AC 151 ms
81,564 KB
testcase_22 AC 160 ms
77,784 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# Union Find 

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def unite(self, x, 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):
        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(N*2)
for i in range(M):
    a, b = map(int, input().split())
    UF.unite(a-1, b-1)

from collections import defaultdict
odd = 0
for members in UF.all_group_members().values():
    if len(members)%2 == 1:
        odd += 1
ans = odd//2
print(ans)
0