結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 75 ms
71,184 KB
testcase_01 AC 74 ms
71,224 KB
testcase_02 AC 73 ms
71,216 KB
testcase_03 AC 77 ms
71,252 KB
testcase_04 AC 75 ms
71,316 KB
testcase_05 AC 76 ms
71,052 KB
testcase_06 AC 74 ms
71,060 KB
testcase_07 AC 73 ms
71,224 KB
testcase_08 AC 292 ms
125,352 KB
testcase_09 AC 72 ms
71,188 KB
testcase_10 AC 344 ms
110,424 KB
testcase_11 AC 136 ms
106,468 KB
testcase_12 AC 306 ms
104,776 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 115 ms
93,152 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 276 ms
107,708 KB
testcase_25 AC 574 ms
102,556 KB
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
権限があれば一括ダウンロードができます

ソースコード

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]=set()
    else:
      for j in member[rx]:
        member[ry].add(j)
      member[rx]=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