結果

問題 No.1451 集団登校
ユーザー 👑 rin204rin204
提出日時 2022-04-15 15:56:58
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 828 bytes
コンパイル時間 245 ms
コンパイル使用メモリ 86,880 KB
実行使用メモリ 97,104 KB
最終ジャッジ日時 2023-08-26 05:55:56
合計ジャッジ時間 7,030 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 62 ms
71,012 KB
testcase_01 AC 64 ms
71,124 KB
testcase_02 AC 61 ms
71,436 KB
testcase_03 AC 61 ms
70,956 KB
testcase_04 AC 61 ms
71,008 KB
testcase_05 AC 63 ms
71,456 KB
testcase_06 AC 61 ms
71,200 KB
testcase_07 AC 63 ms
71,364 KB
testcase_08 AC 195 ms
97,104 KB
testcase_09 AC 65 ms
71,408 KB
testcase_10 WA -
testcase_11 AC 99 ms
89,168 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 84 ms
84,128 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 #

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]:
        leader[x], leader[y] = leader[y], leader[x]
        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

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

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