結果

問題 No.1451 集団登校
ユーザー rlangevinrlangevin
提出日時 2024-01-17 08:58:52
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,706 bytes
コンパイル時間 329 ms
コンパイル使用メモリ 81,572 KB
実行使用メモリ 118,956 KB
最終ジャッジ日時 2024-01-17 08:59:01
合計ジャッジ時間 8,191 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
53,460 KB
testcase_01 AC 35 ms
53,460 KB
testcase_02 AC 35 ms
53,460 KB
testcase_03 AC 39 ms
53,460 KB
testcase_04 AC 35 ms
53,460 KB
testcase_05 AC 35 ms
53,460 KB
testcase_06 AC 36 ms
53,460 KB
testcase_07 AC 36 ms
53,460 KB
testcase_08 AC 245 ms
118,956 KB
testcase_09 AC 35 ms
53,460 KB
testcase_10 WA -
testcase_11 AC 100 ms
105,900 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 88 ms
93,388 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 #

import sys
input = sys.stdin.readline
mod = 10 ** 9 + 7
inv = pow(2, mod - 2, mod)

class UnionFind():
    def __init__(self, n=1):
        self.par = [i for i in range(n)]
        self.rank = [0 for _ in range(n)]
        self.size = [1 for _ in range(n)]
        self.st = [set([i]) for i in range(n)]
        self.ans = [1 for _ in range(n)]

    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x != y:
            if self.rank[x] < self.rank[y]:
                x, y = y, x
            elif self.rank[x] == self.rank[y]:
                for sx in self.st[x]:
                    self.ans[sx] *= inv
                    self.ans[sx] %= mod
                for sy in self.st[y]:
                    self.ans[sy] *= inv
                    self.ans[sy] %= mod                    
                
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
            if self.size[x] != self.size[y]:
                for sy in self.st[y]:
                    self.ans[sy] = 0
            self.par[y] = x
            self.size[x] += self.size[y]
            while self.st[y]:
                self.st[x].add(self.st[y].pop())                

    def is_same(self, x, y):
        return self.find(x) == self.find(y)

    def get_size(self, x):
        x = self.find(x)
        return self.size[x]
    
N, M = map(int, input().split())
U = UnionFind(N)
for i in range(M):
    a, b = map(int, input().split())
    a, b = a - 1, b - 1
    U.union(a, b)
    
for a in U.ans:
    print(a)
0