結果

問題 No.1451 集団登校
ユーザー あかりきあかりき
提出日時 2021-04-13 01:07:31
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 934 ms / 2,000 ms
コード長 1,756 bytes
コンパイル時間 589 ms
コンパイル使用メモリ 86,992 KB
実行使用メモリ 125,104 KB
最終ジャッジ日時 2023-09-11 05:44:23
合計ジャッジ時間 13,353 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 75 ms
71,004 KB
testcase_01 AC 76 ms
71,040 KB
testcase_02 AC 77 ms
71,204 KB
testcase_03 AC 76 ms
71,432 KB
testcase_04 AC 75 ms
71,320 KB
testcase_05 AC 76 ms
71,400 KB
testcase_06 AC 77 ms
71,392 KB
testcase_07 AC 74 ms
71,260 KB
testcase_08 AC 299 ms
125,104 KB
testcase_09 AC 78 ms
71,096 KB
testcase_10 AC 359 ms
110,628 KB
testcase_11 AC 139 ms
106,592 KB
testcase_12 AC 317 ms
104,924 KB
testcase_13 AC 673 ms
118,456 KB
testcase_14 AC 655 ms
115,316 KB
testcase_15 AC 528 ms
103,116 KB
testcase_16 AC 607 ms
114,244 KB
testcase_17 AC 258 ms
80,952 KB
testcase_18 AC 429 ms
95,832 KB
testcase_19 AC 119 ms
93,624 KB
testcase_20 AC 934 ms
121,576 KB
testcase_21 AC 168 ms
78,324 KB
testcase_22 AC 393 ms
86,544 KB
testcase_23 AC 128 ms
78,524 KB
testcase_24 AC 287 ms
108,024 KB
testcase_25 AC 608 ms
104,592 KB
testcase_26 AC 634 ms
114,548 KB
testcase_27 AC 450 ms
96,936 KB
testcase_28 AC 486 ms
93,996 KB
testcase_29 AC 676 ms
104,744 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UF():
  def __init__(self, N):
    self._parent=[n for n in range(0, N)]
    self._size=[1] * N

  def find_root(self, x):
    if self._parent[x]==x:return x
    self._parent[x]=self.find_root(self._parent[x])
    return self._parent[x]

  def unite(self, x, y):
    gx=self.find_root(x)
    gy=self.find_root(y)
    if gx==gy:return

    if self._size[gx]<self._size[gy]:
      self._parent[gx]=gy
      self._size[gy]+=self._size[gx]
    else:
      self._parent[gy]=gx
      self._size[gx]+=self._size[gy]

  def size(self, x):
    return self._size[self.find_root(x)]

  def samegroup(self, x, y):
    return self.find_root(x)==self.find_root(y)

  def groupnum(self):
    N=len(self._parent)
    ans=0
    for i in range(N):
      if self.find_root(i)==i:
        ans+=1
    return ans

n,m=map(int,input().split())
UFa=UF(n)
ans=[1]*n
member=[{i} for i in range(n)]
mod=10**9+7
inv2=pow(2,mod-2,mod)

for i in range(m):
  x,y=map(int,input().split())
  X=UFa.size(x-1)
  Y=UFa.size(y-1)
  
  if X==Y:
    rx=UFa.find_root(x-1)
    ry=UFa.find_root(y-1)
    if rx!=ry:
      UFa.unite(x-1,y-1)
      r=UFa.find_root(x-1)
      for j in member[ry]:
        member[rx].add(j)
      member[ry]=set()
      for j in member[r]:
        ans[j]*=inv2
        ans[j]%=mod
  
  elif X>Y:
    rx=UFa.find_root(x-1)
    ry=UFa.find_root(y-1)
    for j in member[ry]:
      ans[j]=0
    UFa.unite(x-1,y-1)
    r=UFa.find_root(x-1)
    for j in member[ry]:
      member[rx].add(j)
    member[ry]=set()

  else:
    rx=UFa.find_root(x-1)
    ry=UFa.find_root(y-1)
    for j in member[rx]:
      ans[j]=0
    UFa.unite(x-1,y-1)
    r=UFa.find_root(x-1)
    for j in member[rx]:
      member[ry].add(j)
    member[rx]=set()
  
for i in range(n):
  print(ans[i])
0