結果

問題 No.2418 情報通だよ!Nafmoくん
ユーザー sepa38
提出日時 2023-07-15 14:07:29
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 643 ms / 2,000 ms
コード長 730 bytes
コンパイル時間 354 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 20,224 KB
最終ジャッジ日時 2024-11-14 12:20:05
合計ジャッジ時間 9,355 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(500000)
class UF:
  def __init__(self, n):
    self.root = [i for i in range(n)]
    self.subt = [1] * n

  def find(self, x):
    if self.root[x] == x:
      return x
    else:
      self.root[x] = self.find(self.root[x])
      return self.root[x]

  def union(self, x, y):
    x, y = self.find(x), self.find(y)
    x, y = min(x, y), max(x, y)
    if x != y:
      self.subt[x] += self.subt[y]
    self.root[y] = x

  def size(self, x):
    return self.subt[self.find(x)]


n, m = map(int, input().split())
uf = UF(n*2)
for _ in range(m):
  a, b = map(lambda x: int(x)-1, input().split())
  uf.union(a, b)
ans = 0
for i in range(n*2):
  ans += (uf.find(i) == i) * (uf.size(i) % 2)
print(ans // 2)
0