結果

問題 No.1451 集団登校
ユーザー rlangevinrlangevin
提出日時 2024-01-17 12:12:05
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,680 bytes
コンパイル時間 300 ms
コンパイル使用メモリ 81,572 KB
実行使用メモリ 341,172 KB
最終ジャッジ日時 2024-01-17 12:12:13
合計ジャッジ時間 6,760 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
60,264 KB
testcase_01 AC 35 ms
53,460 KB
testcase_02 AC 34 ms
53,460 KB
testcase_03 AC 35 ms
53,460 KB
testcase_04 AC 35 ms
53,460 KB
testcase_05 AC 38 ms
53,460 KB
testcase_06 AC 35 ms
53,460 KB
testcase_07 AC 35 ms
53,460 KB
testcase_08 AC 234 ms
118,296 KB
testcase_09 AC 36 ms
53,460 KB
testcase_10 AC 183 ms
105,364 KB
testcase_11 AC 99 ms
105,888 KB
testcase_12 AC 189 ms
102,336 KB
testcase_13 AC 1,009 ms
191,720 KB
testcase_14 TLE -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
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.size[x] == self.size[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                    
            elif self.size[x] < self.size[y]:
                for sx in self.st[x]:
                    self.ans[sx] = 0
            else:
                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