結果

問題 No.1451 集団登校
ユーザー 小野寺健小野寺健
提出日時 2021-05-03 10:09:55
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 1,207 bytes
コンパイル時間 92 ms
コンパイル使用メモリ 11,052 KB
実行使用メモリ 14,148 KB
最終ジャッジ日時 2023-09-29 04:29:32
合計ジャッジ時間 8,656 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
8,344 KB
testcase_01 AC 17 ms
8,224 KB
testcase_02 AC 16 ms
8,380 KB
testcase_03 AC 16 ms
8,308 KB
testcase_04 AC 16 ms
8,172 KB
testcase_05 AC 16 ms
8,220 KB
testcase_06 AC 17 ms
8,208 KB
testcase_07 AC 17 ms
8,396 KB
testcase_08 AC 492 ms
12,108 KB
testcase_09 AC 16 ms
8,324 KB
testcase_10 WA -
testcase_11 AC 114 ms
14,148 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 74 ms
12,000 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

def modinv(a, m):
    b, u, v = m, 1, 0
    while b != 0:
        t = a // b
        a -= t * b
        a, b = b, a
        u -= t * v
        u, v = v, u
    u %= m
    if u < 0:
        u += m
    return u

par = []
mem = []
prb = []

def init(n):
    for i in range(n):
        par.append(i)
        mem.append(1)
        prb.append(1)
        
def find(x):
    if par[x] == x:
        return x
    else:
        par[x] = find(par[x])
        return par[x]
    
def unite(x, y):
    x = find(x)
    y = find(y)
    if x == y:
        return
    if mem[x] > mem[y]:
        par[y] = x
        mem[x] += mem[y]
        mem[y] = 0
        prb[y] = 0
    else:
        par[x] = y
        if mem[x] == mem[y]:
            prb[y] += prb[x]
        else:
            prb[x] = 0
        mem[y] += mem[x]
            
def prob(x):
    if prb[x] == 0:
        return 0
    if par[x] == x:
        return prb[x]
    else:
        return prob(par[x])

N, M = map(int, input().split())

init(N)

for _ in range(M):
    a, b = map(int, input().split())
    unite(a-1, b-1)
    
    
MOD = 10**9+7
    
for i in range(N):
    p = prob(i)
    if p == 0 or p == 1:
        print(p)
    else:
        print(modinv(p, MOD))
0