結果

問題 No.2418 情報通だよ!Nafmoくん
ユーザー hotate29hotate29
提出日時 2023-08-12 13:53:06
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 525 ms / 2,000 ms
コード長 1,086 bytes
コンパイル時間 85 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 26,240 KB
最終ジャッジ日時 2024-04-30 04:21:56
合計ジャッジ時間 7,794 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 32 ms
10,752 KB
testcase_01 AC 32 ms
10,752 KB
testcase_02 AC 31 ms
10,752 KB
testcase_03 AC 492 ms
25,856 KB
testcase_04 AC 152 ms
14,592 KB
testcase_05 AC 329 ms
20,992 KB
testcase_06 AC 346 ms
21,504 KB
testcase_07 AC 347 ms
21,504 KB
testcase_08 AC 363 ms
21,888 KB
testcase_09 AC 432 ms
23,936 KB
testcase_10 AC 525 ms
25,856 KB
testcase_11 AC 440 ms
23,808 KB
testcase_12 AC 309 ms
20,096 KB
testcase_13 AC 165 ms
15,232 KB
testcase_14 AC 273 ms
18,944 KB
testcase_15 AC 325 ms
20,736 KB
testcase_16 AC 505 ms
26,240 KB
testcase_17 AC 170 ms
15,360 KB
testcase_18 AC 285 ms
19,328 KB
testcase_19 AC 131 ms
13,568 KB
testcase_20 AC 351 ms
21,888 KB
testcase_21 AC 194 ms
16,128 KB
testcase_22 AC 199 ms
16,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind:
    def __init__(
        self,
        n: int,
    ) -> None:
        self.forest = [-1] * n

    def union(self, x: int, y: int) -> None:
        x = self.findRoot(x)
        y = self.findRoot(y)
        if x == y:
            return
        if self.forest[x] > self.forest[y]:
            x, y = y, x
        self.forest[x] += self.forest[y]
        self.forest[y] = x

    def findRoot(self, x: int) -> int:
        if self.forest[x] < 0:
            return x
        else:
            self.forest[x] = self.findRoot(self.forest[x])
            return self.forest[x]

    def issame(self, x: int, y: int) -> bool:
        return self.findRoot(x) == self.findRoot(y)

    def size(self, x: int) -> int:
        return -self.forest[self.findRoot(x)]


n, m = map(int, input().split())
ab = [tuple(map(int, input().split())) for _ in range(m)]

uf = UnionFind(n * 2)
for a, b in ab:
    uf.union(a - 1, b - 1)

groups = []
for i in range(n * 2):
    if i == uf.findRoot(i):
        groups.append(uf.size(i))

ans = 0
for g in groups:
    ans += g % 2

print(ans // 2)
0