結果

問題 No.2418 情報通だよ!Nafmoくん
ユーザー とりゐとりゐ
提出日時 2023-08-12 13:32:03
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 211 ms / 2,000 ms
コード長 1,246 bytes
コンパイル時間 240 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 87,296 KB
最終ジャッジ日時 2024-04-30 03:21:46
合計ジャッジ時間 4,380 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 47 ms
54,016 KB
testcase_01 AC 49 ms
53,888 KB
testcase_02 AC 46 ms
54,016 KB
testcase_03 AC 165 ms
84,224 KB
testcase_04 AC 96 ms
82,336 KB
testcase_05 AC 197 ms
77,184 KB
testcase_06 AC 137 ms
85,120 KB
testcase_07 AC 183 ms
77,184 KB
testcase_08 AC 135 ms
87,296 KB
testcase_09 AC 209 ms
77,952 KB
testcase_10 AC 174 ms
82,560 KB
testcase_11 AC 157 ms
82,432 KB
testcase_12 AC 138 ms
82,048 KB
testcase_13 AC 131 ms
77,568 KB
testcase_14 AC 142 ms
79,744 KB
testcase_15 AC 100 ms
75,904 KB
testcase_16 AC 211 ms
79,872 KB
testcase_17 AC 117 ms
76,416 KB
testcase_18 AC 129 ms
83,328 KB
testcase_19 AC 85 ms
81,024 KB
testcase_20 AC 108 ms
76,032 KB
testcase_21 AC 133 ms
78,464 KB
testcase_22 AC 151 ms
76,672 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from sys import stdin
input=lambda :stdin.readline()[:-1]

from collections import defaultdict

class UnionFind():
  def __init__(self,n):
    self.n=n
    self.parents=[-1]*n
    self.group_count=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
    self.group_count-=1

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

  def same(self,x,y):
    return self.find(x)==self.find(y)

  def members(self,x):
    root=self.find(x)
    return [i for i in range(self.n) if self.find(i)==root]

  def roots(self):
    return [i for i, x in enumerate(self.parents) if x< 0]

  def groups(self):
    group_members=defaultdict(list)
    for member in range(self.n):
      group_members[self.find(member)].append(member)
    return group_members


n,m=map(int,input().split())
uf=UnionFind(2*n)
for i in range(m):
  a,b=map(lambda x:int(x)-1,input().split())
  uf.union(a,b)

ok=0
for i in uf.roots():
  s=uf.size(i)
  ok+=s//2

print(n-ok)
0