結果

問題 No.1451 集団登校
ユーザー rlangevinrlangevin
提出日時 2024-01-17 12:13:52
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 457 ms / 2,000 ms
コード長 1,725 bytes
コンパイル時間 436 ms
コンパイル使用メモリ 81,572 KB
実行使用メモリ 118,168 KB
最終ジャッジ日時 2024-01-17 12:14:01
合計ジャッジ時間 7,660 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
53,460 KB
testcase_01 AC 37 ms
53,460 KB
testcase_02 AC 41 ms
53,460 KB
testcase_03 AC 40 ms
53,460 KB
testcase_04 AC 37 ms
53,460 KB
testcase_05 AC 38 ms
53,460 KB
testcase_06 AC 38 ms
53,460 KB
testcase_07 AC 37 ms
53,460 KB
testcase_08 AC 226 ms
118,168 KB
testcase_09 AC 37 ms
53,460 KB
testcase_10 AC 185 ms
105,364 KB
testcase_11 AC 106 ms
105,888 KB
testcase_12 AC 204 ms
103,104 KB
testcase_13 AC 353 ms
110,568 KB
testcase_14 AC 419 ms
111,008 KB
testcase_15 AC 293 ms
96,248 KB
testcase_16 AC 382 ms
106,836 KB
testcase_17 AC 151 ms
78,496 KB
testcase_18 AC 213 ms
88,128 KB
testcase_19 AC 84 ms
93,380 KB
testcase_20 AC 457 ms
110,700 KB
testcase_21 AC 103 ms
76,604 KB
testcase_22 AC 196 ms
81,556 KB
testcase_23 AC 57 ms
70,580 KB
testcase_24 AC 174 ms
104,788 KB
testcase_25 AC 294 ms
95,316 KB
testcase_26 AC 369 ms
105,968 KB
testcase_27 AC 309 ms
93,960 KB
testcase_28 AC 226 ms
84,956 KB
testcase_29 AC 302 ms
94,972 KB
権限があれば一括ダウンロードができます

ソースコード

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
                x, y = y, x
            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