結果

問題 No.2418 情報通だよ!Nafmoくん
ユーザー 👑 H20H20
提出日時 2023-08-12 13:43:52
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 280 ms / 2,000 ms
コード長 1,615 bytes
コンパイル時間 206 ms
コンパイル使用メモリ 82,396 KB
実行使用メモリ 118,144 KB
最終ジャッジ日時 2024-04-30 03:57:50
合計ジャッジ時間 5,401 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
54,144 KB
testcase_01 AC 41 ms
54,016 KB
testcase_02 AC 40 ms
54,400 KB
testcase_03 AC 265 ms
112,860 KB
testcase_04 AC 142 ms
103,540 KB
testcase_05 AC 241 ms
89,064 KB
testcase_06 AC 220 ms
108,672 KB
testcase_07 AC 224 ms
88,972 KB
testcase_08 AC 230 ms
118,144 KB
testcase_09 AC 263 ms
93,296 KB
testcase_10 AC 262 ms
101,444 KB
testcase_11 AC 237 ms
98,664 KB
testcase_12 AC 188 ms
95,128 KB
testcase_13 AC 165 ms
82,792 KB
testcase_14 AC 183 ms
90,016 KB
testcase_15 AC 150 ms
85,504 KB
testcase_16 AC 280 ms
98,264 KB
testcase_17 AC 135 ms
81,092 KB
testcase_18 AC 181 ms
97,520 KB
testcase_19 AC 112 ms
96,768 KB
testcase_20 AC 164 ms
84,992 KB
testcase_21 AC 171 ms
84,992 KB
testcase_22 AC 182 ms
82,748 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# UnionFind 参考は以下のサイト
# https://note.nkmk.me/python-union-find/
from collections import defaultdict

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 union(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())
AB = [list(map(int, input().split())) for _ in range(M)]
uf = UnionFind(2*N)
for a,b in AB:
    uf.union(a-1,b-1)
ans2 = 0
GM = uf.all_group_members()
for k,v in GM.items():
    ans2+=len(v)%2
print(ans2//2)
0