結果

問題 No.2072 Anatomy
ユーザー とりゐとりゐ
提出日時 2022-09-16 21:39:09
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 430 ms / 2,000 ms
コード長 1,310 bytes
コンパイル時間 308 ms
コンパイル使用メモリ 86,644 KB
実行使用メモリ 95,312 KB
最終ジャッジ日時 2023-08-23 14:37:30
合計ジャッジ時間 9,202 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 89 ms
71,476 KB
testcase_01 AC 88 ms
71,188 KB
testcase_02 AC 93 ms
72,156 KB
testcase_03 AC 103 ms
72,092 KB
testcase_04 AC 95 ms
72,100 KB
testcase_05 AC 99 ms
71,796 KB
testcase_06 AC 98 ms
72,112 KB
testcase_07 AC 98 ms
71,796 KB
testcase_08 AC 430 ms
94,240 KB
testcase_09 AC 252 ms
91,716 KB
testcase_10 AC 378 ms
92,520 KB
testcase_11 AC 309 ms
92,948 KB
testcase_12 AC 266 ms
88,804 KB
testcase_13 AC 353 ms
93,572 KB
testcase_14 AC 336 ms
88,316 KB
testcase_15 AC 224 ms
85,924 KB
testcase_16 AC 379 ms
93,856 KB
testcase_17 AC 291 ms
92,292 KB
testcase_18 AC 212 ms
85,428 KB
testcase_19 AC 358 ms
93,712 KB
testcase_20 AC 427 ms
95,052 KB
testcase_21 AC 255 ms
92,260 KB
testcase_22 AC 365 ms
94,172 KB
testcase_23 AC 256 ms
92,032 KB
testcase_24 AC 249 ms
92,056 KB
testcase_25 AC 342 ms
93,424 KB
testcase_26 AC 89 ms
71,368 KB
testcase_27 AC 254 ms
91,924 KB
testcase_28 AC 402 ms
95,312 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict

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

    if self.parents[x]>self.parents[y]:
      x,y=y,x

    self.parents[x]+=self.parents[y]
    self.parents[y]=x
    mx=max(self.cnt[x],self.cnt[y])
    self.cnt[x]=mx+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 group_count(self):
    return len(self.roots())

  def all_group_members(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())
query=[]
for _ in range(m):
  a,b=map(lambda x:int(x)-1,input().split())
  query.append((a,b))

uf=UnionFind(n)
for a,b in query[::-1]:
  uf.union(a,b)

print(max(uf.cnt))
0