結果

問題 No.1451 集団登校
ユーザー あかりきあかりき
提出日時 2021-04-13 00:46:23
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 1,817 bytes
コンパイル時間 296 ms
コンパイル使用メモリ 86,652 KB
実行使用メモリ 124,612 KB
最終ジャッジ日時 2023-09-11 05:23:42
合計ジャッジ時間 11,064 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 75 ms
71,212 KB
testcase_01 AC 75 ms
71,080 KB
testcase_02 AC 75 ms
71,124 KB
testcase_03 AC 78 ms
71,000 KB
testcase_04 AC 75 ms
71,124 KB
testcase_05 AC 73 ms
71,140 KB
testcase_06 AC 77 ms
71,236 KB
testcase_07 AC 74 ms
71,328 KB
testcase_08 AC 336 ms
124,612 KB
testcase_09 AC 74 ms
70,988 KB
testcase_10 AC 355 ms
110,512 KB
testcase_11 AC 141 ms
106,496 KB
testcase_12 AC 310 ms
104,536 KB
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 AC 116 ms
93,104 KB
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 AC 277 ms
107,716 KB
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
権限があれば一括ダウンロードができます

ソースコード

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)
    UFa.unite(x-1,y-1)
    r=UFa.find_root(x-1)
    if r==rx:
      for j in member[ry]:
        member[rx].add(j)
      member[ry]={}
    else:
      for j in member[rx]:
        member[ry].add(j)
      member[rx]={}
    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]={}

  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]={}

for i in range(n):
  print(ans[i])
0