結果

問題 No.2418 情報通だよ!Nafmoくん
ユーザー maru65536maru65536
提出日時 2023-08-12 13:48:18
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 434 ms / 2,000 ms
コード長 935 bytes
コンパイル時間 158 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 20,480 KB
最終ジャッジ日時 2024-04-30 04:07:52
合計ジャッジ時間 6,501 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 31 ms
10,624 KB
testcase_01 AC 32 ms
10,752 KB
testcase_02 AC 31 ms
10,880 KB
testcase_03 AC 415 ms
18,688 KB
testcase_04 AC 107 ms
18,432 KB
testcase_05 AC 292 ms
12,416 KB
testcase_06 AC 284 ms
18,560 KB
testcase_07 AC 310 ms
11,904 KB
testcase_08 AC 280 ms
20,480 KB
testcase_09 AC 370 ms
12,800 KB
testcase_10 AC 400 ms
17,280 KB
testcase_11 AC 356 ms
17,024 KB
testcase_12 AC 256 ms
16,256 KB
testcase_13 AC 136 ms
12,672 KB
testcase_14 AC 231 ms
14,592 KB
testcase_15 AC 303 ms
11,008 KB
testcase_16 AC 434 ms
14,592 KB
testcase_17 AC 156 ms
10,752 KB
testcase_18 AC 224 ms
17,408 KB
testcase_19 AC 87 ms
17,792 KB
testcase_20 AC 332 ms
10,880 KB
testcase_21 AC 162 ms
13,440 KB
testcase_22 AC 176 ms
11,008 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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 roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

n,m=map(int,input().split())
UF=UnionFind(2*n)
for _ in range(m):
    a,b=map(int,input().split())
    UF.union(a-1,b-1)
print(sum([UF.size(i)%2 for i in UF.roots()])//2)
0