結果

問題 No.2418 情報通だよ!Nafmoくん
ユーザー aaaaaaaaaa2230aaaaaaaaaa2230
提出日時 2023-08-12 13:48:15
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 291 ms / 2,000 ms
コード長 897 bytes
コンパイル時間 291 ms
コンパイル使用メモリ 82,364 KB
実行使用メモリ 81,024 KB
最終ジャッジ日時 2024-04-30 04:08:20
合計ジャッジ時間 5,625 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 66 ms
67,200 KB
testcase_01 AC 67 ms
66,944 KB
testcase_02 AC 69 ms
67,328 KB
testcase_03 AC 239 ms
81,024 KB
testcase_04 AC 123 ms
79,700 KB
testcase_05 AC 274 ms
79,912 KB
testcase_06 AC 190 ms
80,128 KB
testcase_07 AC 241 ms
79,772 KB
testcase_08 AC 188 ms
80,616 KB
testcase_09 AC 273 ms
80,132 KB
testcase_10 AC 243 ms
80,768 KB
testcase_11 AC 220 ms
80,256 KB
testcase_12 AC 185 ms
79,360 KB
testcase_13 AC 178 ms
79,204 KB
testcase_14 AC 195 ms
79,848 KB
testcase_15 AC 165 ms
78,660 KB
testcase_16 AC 291 ms
80,212 KB
testcase_17 AC 169 ms
79,360 KB
testcase_18 AC 174 ms
79,872 KB
testcase_19 AC 113 ms
79,336 KB
testcase_20 AC 164 ms
78,592 KB
testcase_21 AC 173 ms
79,360 KB
testcase_22 AC 200 ms
79,368 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from typing import Union


class Unionfind:
     
    def __init__(self,n):
        self.uf = [-1]*n
 
    def find(self,x):
        if self.uf[x] < 0:
            return x
        else:
            self.uf[x] = self.find(self.uf[x])
            return self.uf[x]
 
    def same(self,x,y):
        return self.find(x) == self.find(y)
 
    def union(self,x,y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return False
        if self.uf[x] > self.uf[y]:
            x,y = y,x
        self.uf[x] += self.uf[y]
        self.uf[y] = x
        return True
 
    def size(self,x):
        x = self.find(x)
        return -self.uf[x]


n,m = map(int,input().split())

uf = Unionfind(2*n)
for _ in range(m):
    a,b = [int(x)-1 for x in input().split()]
    uf.union(a,b)

ans = 0
for i in range(2*n):
    if uf.find(i) == i:
        ans += uf.size(i)%2
print(ans//2)
0