結果

問題 No.1451 集団登校
ユーザー nasubi24nasubi24
提出日時 2021-03-31 15:18:28
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 570 ms / 2,000 ms
コード長 1,462 bytes
コンパイル時間 448 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 95,384 KB
最終ジャッジ日時 2024-05-08 17:43:18
合計ジャッジ時間 9,073 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
52,096 KB
testcase_01 AC 44 ms
52,224 KB
testcase_02 AC 44 ms
52,480 KB
testcase_03 AC 44 ms
52,352 KB
testcase_04 AC 43 ms
52,096 KB
testcase_05 AC 44 ms
52,480 KB
testcase_06 AC 45 ms
52,096 KB
testcase_07 AC 43 ms
52,480 KB
testcase_08 AC 212 ms
93,208 KB
testcase_09 AC 43 ms
52,352 KB
testcase_10 AC 302 ms
92,704 KB
testcase_11 AC 103 ms
87,936 KB
testcase_12 AC 300 ms
89,828 KB
testcase_13 AC 521 ms
95,384 KB
testcase_14 AC 500 ms
91,392 KB
testcase_15 AC 416 ms
87,416 KB
testcase_16 AC 406 ms
90,232 KB
testcase_17 AC 236 ms
78,628 KB
testcase_18 AC 291 ms
83,684 KB
testcase_19 AC 89 ms
83,072 KB
testcase_20 AC 512 ms
90,852 KB
testcase_21 AC 147 ms
77,184 KB
testcase_22 AC 337 ms
81,396 KB
testcase_23 AC 100 ms
76,032 KB
testcase_24 AC 222 ms
90,312 KB
testcase_25 AC 372 ms
85,236 KB
testcase_26 AC 395 ms
89,932 KB
testcase_27 AC 423 ms
84,552 KB
testcase_28 AC 258 ms
80,624 KB
testcase_29 AC 570 ms
88,428 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