結果
| 問題 |
No.2418 情報通だよ!Nafmoくん
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-09-22 15:51:31 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 181 ms / 2,000 ms |
| コード長 | 1,088 bytes |
| コンパイル時間 | 225 ms |
| コンパイル使用メモリ | 82,396 KB |
| 実行使用メモリ | 78,280 KB |
| 最終ジャッジ日時 | 2024-11-14 12:26:16 |
| 合計ジャッジ時間 | 4,115 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 21 |
ソースコード
class UnionFind:
def __init__(self, n):
self.n = n
self.par = [-1] * n
self.group_ = n
def find(self, x):
if self.par[x] < 0:
return x
lst = []
while self.par[x] >= 0:
lst.append(x)
x = self.par[x]
for y in lst:
self.par[y] = x
return x
def unite(self, x, y):
x = self.find(x)
y = self.find(y)
if x == y:
return False
if self.par[x] > self.par[y]:
x, y = y, x
self.par[x] += self.par[y]
self.par[y] = x
self.group_ -= 1
return True
def size(self, x):
return -self.par[self.find(x)]
def same(self, x, y):
return self.find(x) == self.find(y)
@property
def group(self):
return self.group_
n, m = map(int, input().split())
UF = UnionFind(2 * n)
for _ in range(m):
a, b = map(int, input().split())
UF.unite(a - 1, b - 1)
ans = 0
for i in range(2 * n):
if i == UF.find(i):
ans += UF.size(i) & 1
print(ans // 2)