結果

問題 No.1451 集団登校
ユーザー tamatotamato
提出日時 2021-03-31 15:47:29
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,608 bytes
コンパイル時間 396 ms
コンパイル使用メモリ 87,340 KB
実行使用メモリ 101,992 KB
最終ジャッジ日時 2023-08-21 13:31:31
合計ジャッジ時間 11,179 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 95 ms
72,020 KB
testcase_01 AC 95 ms
71,688 KB
testcase_02 AC 94 ms
71,932 KB
testcase_03 AC 94 ms
71,672 KB
testcase_04 AC 94 ms
71,732 KB
testcase_05 AC 93 ms
71,884 KB
testcase_06 AC 94 ms
71,804 KB
testcase_07 AC 96 ms
71,872 KB
testcase_08 AC 358 ms
101,992 KB
testcase_09 AC 96 ms
71,744 KB
testcase_10 WA -
testcase_11 AC 196 ms
84,448 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 156 ms
79,696 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 = 1000000007
eps = 10**-9


def main():
    import sys
    from collections import deque
    input = sys.stdin.buffer.readline

    class UnionFind():
        def __init__(self, n):
            self.n = n
            self.root = [-1] * (n + 1)
            self.rnk = [0] * (n + 1)

        def find_root(self, x):
            while self.root[x] >= 0:
                x = self.root[x]
            return x

        def unite(self, x, y):
            x = self.find_root(x)
            y = self.find_root(y)
            if x == y:
                return
            elif self.rnk[x] > self.rnk[y]:
                self.root[x] += self.root[y]
                self.root[y] = x
            else:
                self.root[y] += self.root[x]
                self.root[x] = y
                if self.rnk[x] == self.rnk[y]:
                    self.rnk[y] += 1

        def isSameGroup(self, x, y):
            return self.find_root(x) == self.find_root(y)

        def size(self, x):
            return -self.root[self.find_root(x)]

    N, M = map(int, input().split())
    ans = [1] * (N+1)
    UF = UnionFind(N)
    adj = [[] for _ in range(N+1)]
    for _ in range(M):
        a, b = map(int, input().split())
        if not UF.isSameGroup(a, b):
            sa = UF.size(a)
            sb = UF.size(b)
            if sa == sb:
                adj[a].append(b)
                adj[b].append(a)
                que = deque()
                que.append(a)
                seen = {a}
                while que:
                    v = que.popleft()
                    ans[v] *= 2
                    for u in adj[v]:
                        if u not in seen:
                            que.append(u)
                            seen.add(u)
            elif sa < sb:
                que = deque()
                que.append(a)
                seen = {a}
                while que:
                    v = que.popleft()
                    ans[v] *= 0
                    for u in adj[v]:
                        if u not in seen:
                            que.append(u)
                            seen.add(u)
            else:
                que = deque()
                que.append(b)
                seen = {b}
                while que:
                    v = que.popleft()
                    ans[v] *= 0
                    for u in adj[v]:
                        if u not in seen:
                            que.append(u)
                            seen.add(u)
            UF.unite(a, b)
    for i in range(1, N+1):
        print(pow(ans[i], mod-2, mod))


if __name__ == '__main__':
    main()
0