結果

問題 No.1451 集団登校
ユーザー aaaaaaaaaa2230aaaaaaaaaa2230
提出日時 2021-05-03 22:50:21
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,209 bytes
コンパイル時間 1,140 ms
コンパイル使用メモリ 86,780 KB
実行使用メモリ 94,572 KB
最終ジャッジ日時 2023-09-29 19:00:01
合計ジャッジ時間 9,458 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 69 ms
70,908 KB
testcase_01 AC 70 ms
71,388 KB
testcase_02 AC 70 ms
71,112 KB
testcase_03 AC 69 ms
71,280 KB
testcase_04 AC 68 ms
71,288 KB
testcase_05 AC 70 ms
71,108 KB
testcase_06 AC 71 ms
71,344 KB
testcase_07 AC 69 ms
71,304 KB
testcase_08 AC 197 ms
94,572 KB
testcase_09 AC 68 ms
71,232 KB
testcase_10 WA -
testcase_11 AC 113 ms
88,552 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 358 ms
91,116 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 195 ms
78,868 KB
testcase_18 WA -
testcase_19 AC 100 ms
83,840 KB
testcase_20 AC 350 ms
91,784 KB
testcase_21 AC 141 ms
78,332 KB
testcase_22 AC 191 ms
79,868 KB
testcase_23 AC 103 ms
77,416 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 286 ms
86,164 KB
testcase_28 AC 219 ms
81,444 KB
testcase_29 AC 286 ms
87,292 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class Unionfind:
     
    def __init__(self,n):
        self.uf = [-1]*n
        self.leader = [[i] for i in range(n)]
        self.prob = [1]*n
 
    def find(self,x):
        if self.uf[x] < 0:
            return x
        else:
            self.uf[x] = self.find(self.uf[x])
            return self.uf[x]
 
    def same(self,x,y):
        return self.find(x) == self.find(y)
 
    def union(self,x,y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return False
        if self.uf[x] > self.uf[y]:
            x,y = y,x
        if self.uf[x] == self.uf[y]:
            self.prob[x] *= 500000004
            self.prob[x] %= mod
            self.leader[x] += self.leader[y]
        self.uf[x] += self.uf[y]
        self.uf[y] = x
        self.leader[y] = []
        self.prob[y] = 0
        return True
 
    def size(self,x):
        x = self.find(x)
        return -self.uf[x]

n,m = map(int,input().split())
uf = Unionfind(n)
mod = 10**9+7
for i in range(m):
    a,b = map(int,input().split())
    uf.union(a-1,b-1)

ans = [0]*n
for i in range(n):
    if uf.find(i) != i:
        continue
    for j in uf.leader[i]:
        ans[j] = uf.prob[i]
for i in ans:
    print(i)
0