結果
問題 | No.2418 情報通だよ!Nafmoくん |
ユーザー |
|
提出日時 | 2023-08-12 13:48:15 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 258 ms / 2,000 ms |
コード長 | 897 bytes |
コンパイル時間 | 144 ms |
コンパイル使用メモリ | 81,836 KB |
実行使用メモリ | 80,848 KB |
最終ジャッジ日時 | 2024-11-14 12:21:43 |
合計ジャッジ時間 | 5,073 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 21 |
ソースコード
from typing import Unionclass Unionfind:def __init__(self,n):self.uf = [-1]*ndef find(self,x):if self.uf[x] < 0:return xelse: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 Falseif self.uf[x] > self.uf[y]:x,y = y,xself.uf[x] += self.uf[y]self.uf[y] = xreturn Truedef 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 = 0for i in range(2*n):if uf.find(i) == i:ans += uf.size(i)%2print(ans//2)