結果

問題 No.1451 集団登校
ユーザー nasubi24nasubi24
提出日時 2021-03-31 15:18:28
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 550 ms / 2,000 ms
コード長 1,462 bytes
コンパイル時間 1,373 ms
コンパイル使用メモリ 87,096 KB
実行使用メモリ 97,864 KB
最終ジャッジ日時 2023-08-21 12:24:24
合計ジャッジ時間 9,523 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
71,348 KB
testcase_01 AC 73 ms
71,328 KB
testcase_02 AC 71 ms
71,160 KB
testcase_03 AC 73 ms
71,380 KB
testcase_04 AC 71 ms
71,256 KB
testcase_05 AC 73 ms
71,048 KB
testcase_06 AC 74 ms
71,036 KB
testcase_07 AC 74 ms
71,204 KB
testcase_08 AC 227 ms
95,516 KB
testcase_09 AC 75 ms
71,252 KB
testcase_10 AC 294 ms
93,160 KB
testcase_11 AC 113 ms
88,880 KB
testcase_12 AC 295 ms
92,460 KB
testcase_13 AC 490 ms
97,864 KB
testcase_14 AC 467 ms
93,464 KB
testcase_15 AC 394 ms
88,712 KB
testcase_16 AC 374 ms
92,120 KB
testcase_17 AC 230 ms
80,872 KB
testcase_18 AC 277 ms
84,636 KB
testcase_19 AC 99 ms
84,312 KB
testcase_20 AC 461 ms
94,732 KB
testcase_21 AC 154 ms
78,728 KB
testcase_22 AC 323 ms
83,624 KB
testcase_23 AC 114 ms
77,344 KB
testcase_24 AC 229 ms
91,524 KB
testcase_25 AC 355 ms
87,340 KB
testcase_26 AC 377 ms
92,036 KB
testcase_27 AC 392 ms
85,928 KB
testcase_28 AC 255 ms
81,588 KB
testcase_29 AC 550 ms
92,656 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFindBySize():
    def __init__(self, n):
        self.parents = list(range(n))
        self.size = [1] * n
    def find(self, x):
        if self.parents[x] == x:
            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:
            return
        if self.size[x] < self.size[y]:
            self.size[y] += self.size[x]
            self.parents[x] = y
        else:
            self.size[x] += self.size[y]
            self.parents[y] = x
n,m=map(int,input().split())
mod=10**9+7
uf=UnionFindBySize(n)
num2=pow(2,mod-2,mod)
ans=[0]*n
size=[1]*n
memo=[[_] for _ in range(n)]
for i in range(m):
    a,b=map(int,input().split())
    a-=1
    b-=1
    e=uf.find(a)
    f=uf.find(b)
    c=size[e]
    d=size[f]
    if e!=f:
        if c>d:
            for j in range(len(memo[f])):
                ans[memo[f][j]]=-1
            memo[f]=[]
            size[e]+=size[f]
        elif d>c:
            for j in range(len(memo[e])):
                ans[memo[e][j]]=-1
            memo[e]=[]
            size[f]+=size[e]
        else:
            memo[e]+=memo[f]
            memo[f]=[]
            for j in range(len(memo[e])):
                ans[memo[e][j]]+=1
            size[e]*=2
        uf.union(a,b)
for i in ans:
    if i==-1:
        print(0)
    else:
        print(pow(num2,i,mod))
0