結果

問題 No.2418 情報通だよ!Nafmoくん
ユーザー nikoro256nikoro256
提出日時 2023-08-12 14:40:00
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 242 ms / 2,000 ms
コード長 1,399 bytes
コンパイル時間 268 ms
コンパイル使用メモリ 82,376 KB
実行使用メモリ 88,320 KB
最終ジャッジ日時 2024-04-30 06:46:22
合計ジャッジ時間 4,617 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
54,100 KB
testcase_01 AC 42 ms
54,144 KB
testcase_02 AC 42 ms
53,888 KB
testcase_03 AC 194 ms
85,760 KB
testcase_04 AC 102 ms
88,072 KB
testcase_05 AC 207 ms
78,720 KB
testcase_06 AC 156 ms
86,748 KB
testcase_07 AC 198 ms
78,208 KB
testcase_08 AC 154 ms
88,320 KB
testcase_09 AC 225 ms
79,488 KB
testcase_10 AC 212 ms
84,608 KB
testcase_11 AC 188 ms
84,224 KB
testcase_12 AC 158 ms
83,088 KB
testcase_13 AC 146 ms
78,336 KB
testcase_14 AC 165 ms
81,208 KB
testcase_15 AC 116 ms
76,752 KB
testcase_16 AC 242 ms
80,768 KB
testcase_17 AC 130 ms
77,484 KB
testcase_18 AC 143 ms
85,580 KB
testcase_19 AC 92 ms
86,548 KB
testcase_20 AC 121 ms
76,680 KB
testcase_21 AC 141 ms
79,360 KB
testcase_22 AC 158 ms
77,824 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict

class UnionFind():

    def __init__(self, n):
        self.n = n
        self.root = [-1]*(n+1)
        self.rank = [0]*(n+1)

    def find(self, x):
        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):
        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):
        return self.find(x) == self.find(y)

    def size(self, x):
        return -self.root[self.find(x)]

    def roots(self):
        return [i for i, x in enumerate(self.root) if x < 0]

    def group_size(self):
        return len(self.roots())

    def group_members(self):
        group_members = defaultdict(list)
        for member in range(self.n):
            group_members[self.find(member)].append(member)
        return group_members

N,M=map(int,input().split())
uf=UnionFind(2*N-1)
for _ in range(M):
    a,b=map(int,input().split())
    uf.unite(a-1,b-1)
ans=0
for r in uf.roots():
    ans+=uf.size(r)%2
print((ans-1)//2+1)
0