結果

問題 No.1451 集団登校
ユーザー あかりきあかりき
提出日時 2021-04-13 01:05:38
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,725 bytes
コンパイル時間 1,676 ms
コンパイル使用メモリ 87,108 KB
実行使用メモリ 125,464 KB
最終ジャッジ日時 2023-09-11 05:42:17
合計ジャッジ時間 12,696 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 78 ms
71,260 KB
testcase_01 AC 77 ms
71,572 KB
testcase_02 AC 78 ms
71,020 KB
testcase_03 AC 79 ms
71,284 KB
testcase_04 AC 76 ms
71,200 KB
testcase_05 AC 77 ms
71,560 KB
testcase_06 AC 77 ms
71,424 KB
testcase_07 AC 76 ms
71,420 KB
testcase_08 AC 309 ms
125,464 KB
testcase_09 AC 77 ms
71,536 KB
testcase_10 AC 350 ms
110,744 KB
testcase_11 AC 139 ms
106,696 KB
testcase_12 AC 308 ms
104,916 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 117 ms
93,544 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 276 ms
107,788 KB
testcase_25 AC 580 ms
103,296 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)
    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