結果

問題 No.1451 集団登校
ユーザー 👑 tamatotamato
提出日時 2021-03-31 15:49:17
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 973 ms / 2,000 ms
コード長 2,740 bytes
コンパイル時間 466 ms
コンパイル使用メモリ 87,328 KB
実行使用メモリ 101,720 KB
最終ジャッジ日時 2023-08-21 13:33:59
合計ジャッジ時間 11,708 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 91 ms
71,904 KB
testcase_01 AC 89 ms
71,648 KB
testcase_02 AC 89 ms
71,756 KB
testcase_03 AC 91 ms
71,392 KB
testcase_04 AC 90 ms
71,380 KB
testcase_05 AC 93 ms
71,832 KB
testcase_06 AC 93 ms
71,504 KB
testcase_07 AC 90 ms
71,784 KB
testcase_08 AC 360 ms
101,720 KB
testcase_09 AC 91 ms
71,924 KB
testcase_10 AC 338 ms
88,124 KB
testcase_11 AC 188 ms
84,416 KB
testcase_12 AC 375 ms
89,252 KB
testcase_13 AC 827 ms
97,008 KB
testcase_14 AC 529 ms
90,748 KB
testcase_15 AC 451 ms
88,104 KB
testcase_16 AC 572 ms
92,380 KB
testcase_17 AC 231 ms
80,528 KB
testcase_18 AC 357 ms
85,124 KB
testcase_19 AC 152 ms
79,896 KB
testcase_20 AC 973 ms
97,208 KB
testcase_21 AC 163 ms
78,828 KB
testcase_22 AC 277 ms
81,444 KB
testcase_23 AC 118 ms
77,196 KB
testcase_24 AC 314 ms
87,496 KB
testcase_25 AC 494 ms
88,020 KB
testcase_26 AC 511 ms
91,436 KB
testcase_27 AC 445 ms
87,440 KB
testcase_28 AC 332 ms
83,376 KB
testcase_29 AC 665 ms
90,420 KB
権限があれば一括ダウンロードができます

ソースコード

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)
                adj[a].append(b)
                adj[b].append(a)
            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)
                adj[a].append(b)
                adj[b].append(a)
            UF.unite(a, b)
    for i in range(1, N+1):
        print(pow(ans[i], mod-2, mod))


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