結果

問題 No.1451 集団登校
ユーザー 👑 rin204rin204
提出日時 2022-04-15 16:15:51
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 406 ms / 2,000 ms
コード長 795 bytes
コンパイル時間 289 ms
コンパイル使用メモリ 87,272 KB
実行使用メモリ 98,724 KB
最終ジャッジ日時 2023-08-26 05:59:08
合計ジャッジ時間 8,540 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 77 ms
71,352 KB
testcase_01 AC 76 ms
71,424 KB
testcase_02 AC 77 ms
71,308 KB
testcase_03 AC 77 ms
71,196 KB
testcase_04 AC 76 ms
71,464 KB
testcase_05 AC 75 ms
71,288 KB
testcase_06 AC 75 ms
71,348 KB
testcase_07 AC 75 ms
71,320 KB
testcase_08 AC 234 ms
98,724 KB
testcase_09 AC 77 ms
71,440 KB
testcase_10 AC 205 ms
91,336 KB
testcase_11 AC 114 ms
89,264 KB
testcase_12 AC 211 ms
90,424 KB
testcase_13 AC 330 ms
94,708 KB
testcase_14 AC 403 ms
92,384 KB
testcase_15 AC 278 ms
87,640 KB
testcase_16 AC 333 ms
92,404 KB
testcase_17 AC 198 ms
78,420 KB
testcase_18 AC 227 ms
85,616 KB
testcase_19 AC 101 ms
84,140 KB
testcase_20 AC 406 ms
92,904 KB
testcase_21 AC 150 ms
78,140 KB
testcase_22 AC 214 ms
79,868 KB
testcase_23 AC 113 ms
77,376 KB
testcase_24 AC 187 ms
91,848 KB
testcase_25 AC 305 ms
88,396 KB
testcase_26 AC 321 ms
93,224 KB
testcase_27 AC 325 ms
86,152 KB
testcase_28 AC 257 ms
81,672 KB
testcase_29 AC 303 ms
87,236 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

MOD = 10 ** 9 + 7

n, m = map(int, input().split())
leader = [[i] for i in range(n)]
parent = [-1] * n
ans = [1] * n
inv = pow(2, MOD - 2, MOD)
def find(x):
    if parent[x] < 0:
        return x
    parent[x] = find(parent[x])
    return parent[x]

def unite(x, y):
    x = find(x)
    y = find(y)
    if x == y:
        return

    if parent[x] > parent[y]:
        x, y = y, x
    
    if parent[x] == parent[y]:
        for i in leader[y]:
            leader[x].append(i)
        
        for i in leader[x]:
            ans[i] *= inv
            ans[i] %= MOD
    else:
        for i in leader[y]:
            ans[i] = 0

    parent[x] += parent[y]
    parent[y] = x
    leader[y] = []

for _ in range(m):
    a, b = map(int, input().split())
    unite(a - 1, b - 1)

print(*ans, sep="\n")
0