結果

問題 No.2418 情報通だよ!Nafmoくん
ユーザー miya145592miya145592
提出日時 2023-08-12 13:49:06
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 396 ms / 2,000 ms
コード長 1,517 bytes
コンパイル時間 223 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 113,280 KB
最終ジャッジ日時 2024-04-30 04:11:04
合計ジャッジ時間 6,441 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
52,352 KB
testcase_01 AC 41 ms
52,656 KB
testcase_02 AC 40 ms
52,352 KB
testcase_03 AC 310 ms
108,076 KB
testcase_04 AC 145 ms
103,444 KB
testcase_05 AC 303 ms
89,756 KB
testcase_06 AC 240 ms
105,728 KB
testcase_07 AC 297 ms
88,108 KB
testcase_08 AC 244 ms
113,280 KB
testcase_09 AC 345 ms
92,544 KB
testcase_10 AC 344 ms
106,444 KB
testcase_11 AC 282 ms
102,228 KB
testcase_12 AC 235 ms
98,912 KB
testcase_13 AC 205 ms
84,688 KB
testcase_14 AC 247 ms
93,184 KB
testcase_15 AC 173 ms
85,672 KB
testcase_16 AC 396 ms
98,244 KB
testcase_17 AC 163 ms
81,692 KB
testcase_18 AC 223 ms
101,192 KB
testcase_19 AC 124 ms
100,480 KB
testcase_20 AC 174 ms
85,572 KB
testcase_21 AC 211 ms
88,076 KB
testcase_22 AC 233 ms
83,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind:
    def __init__(self, n, w=None):
        self.par = [-1]*n
        self.rank = [0]*n
        self.siz = [1]*n
        self.cnt = n
        self.min_node = [i for i in range(n)]
        self.weight = w if w else [1]*n

    def root(self, x):
        if self.par[x] == -1:
            return x
        self.par[x] = self.root(self.par[x])
        return self.par[x]

    def issame(self, x, y):
        return self.root(x) == self.root(y)
            
    def unite(self, x, y):
        px = self.root(x)
        py = self.root(y)
        if px == py:
            return False
        if self.rank[px] < self.rank[py]:
            px, py = py, px
        self.par[py] = px
        if self.rank[px] == self.rank[py]:
            self.rank[px] += 1
        self.siz[px] += self.siz[py]
        self.cnt -= 1
        self.min_node[px] = min(self.min_node[px], self.min_node[py])
        self.weight[px] += self.weight[py]
        return False

    def count(self):
        return self.cnt

    def min(self, x):
        return self.min_node[self.root(x)]

    def getweight(self, x):
        return self.weight[self.root(x)]
    
    def size(self, x):
        return self.siz[self.root(x)]

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:
    a-=1
    b-=1
    UF.unite(a, b)

cnt = dict()
for i in range(2*N):
    r = UF.root(i)
    s = UF.size(r)
    cnt[r] = s%2

ans = 0
for key in cnt:
    ans += cnt[key]
print(ans//2)
0