結果

問題 No.1451 集団登校
ユーザー tamatotamato
提出日時 2021-03-31 15:49:17
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 935 ms / 2,000 ms
コード長 2,740 bytes
コンパイル時間 523 ms
コンパイル使用メモリ 82,560 KB
実行使用メモリ 106,880 KB
最終ジャッジ日時 2024-05-08 18:55:45
合計ジャッジ時間 10,397 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 49 ms
54,144 KB
testcase_01 AC 44 ms
54,400 KB
testcase_02 AC 45 ms
54,400 KB
testcase_03 AC 45 ms
54,400 KB
testcase_04 AC 45 ms
54,400 KB
testcase_05 AC 45 ms
54,400 KB
testcase_06 AC 45 ms
54,400 KB
testcase_07 AC 45 ms
54,272 KB
testcase_08 AC 323 ms
106,880 KB
testcase_09 AC 44 ms
54,272 KB
testcase_10 AC 304 ms
85,984 KB
testcase_11 AC 160 ms
82,688 KB
testcase_12 AC 341 ms
87,064 KB
testcase_13 AC 780 ms
94,072 KB
testcase_14 AC 504 ms
89,472 KB
testcase_15 AC 406 ms
85,448 KB
testcase_16 AC 522 ms
89,200 KB
testcase_17 AC 196 ms
78,320 KB
testcase_18 AC 324 ms
83,348 KB
testcase_19 AC 124 ms
80,256 KB
testcase_20 AC 935 ms
94,728 KB
testcase_21 AC 129 ms
77,312 KB
testcase_22 AC 244 ms
80,280 KB
testcase_23 AC 83 ms
76,288 KB
testcase_24 AC 280 ms
85,444 KB
testcase_25 AC 452 ms
85,788 KB
testcase_26 AC 483 ms
88,948 KB
testcase_27 AC 414 ms
84,740 KB
testcase_28 AC 293 ms
81,524 KB
testcase_29 AC 620 ms
88,688 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