結果

問題 No.1451 集団登校
ユーザー roarisroaris
提出日時 2021-04-01 09:19:27
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 682 ms / 2,000 ms
コード長 1,508 bytes
コンパイル時間 294 ms
コンパイル使用メモリ 87,276 KB
実行使用メモリ 97,952 KB
最終ジャッジ日時 2023-08-22 14:16:44
合計ジャッジ時間 11,552 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,480 KB
testcase_01 AC 71 ms
71,416 KB
testcase_02 AC 72 ms
71,412 KB
testcase_03 AC 72 ms
71,356 KB
testcase_04 AC 72 ms
71,328 KB
testcase_05 AC 73 ms
71,444 KB
testcase_06 AC 72 ms
71,384 KB
testcase_07 AC 69 ms
71,332 KB
testcase_08 AC 205 ms
94,684 KB
testcase_09 AC 72 ms
71,180 KB
testcase_10 AC 311 ms
89,812 KB
testcase_11 AC 182 ms
87,616 KB
testcase_12 AC 309 ms
90,056 KB
testcase_13 AC 611 ms
97,952 KB
testcase_14 AC 404 ms
89,572 KB
testcase_15 AC 389 ms
87,016 KB
testcase_16 AC 552 ms
93,580 KB
testcase_17 AC 242 ms
79,928 KB
testcase_18 AC 349 ms
86,588 KB
testcase_19 AC 142 ms
83,380 KB
testcase_20 AC 682 ms
94,052 KB
testcase_21 AC 148 ms
77,860 KB
testcase_22 AC 260 ms
81,568 KB
testcase_23 AC 100 ms
77,072 KB
testcase_24 AC 287 ms
89,952 KB
testcase_25 AC 449 ms
89,296 KB
testcase_26 AC 522 ms
92,724 KB
testcase_27 AC 501 ms
87,664 KB
testcase_28 AC 271 ms
82,436 KB
testcase_29 AC 482 ms
88,448 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

class Unionfind:
    def __init__(self, n):
        self.par = [-1]*n
    
    def root(self, x):
        r = x
        
        while not self.par[r]<0:
            r = self.par[r]
        
        t = x
        
        while t!=r:
            tmp = t
            t = self.par[t]
            self.par[tmp] = r
        
        return r
    
    def unite(self, x, y):
        rx = self.root(x)
        ry = self.root(y)
        
        if rx==ry:
            return
        
        if -self.par[rx]<=-self.par[ry]:
            self.par[ry] += self.par[rx]
            self.par[rx] = ry
        else:
            self.par[rx] += self.par[ry]
            self.par[ry] = rx
    
    def is_same(self, x, y):
        return self.root(x)==self.root(y)
    
    def count(self, x):
        return -self.par[self.root(x)]

N, M = map(int, input().split())
uf = Unionfind(N)
belong = [[i] for i in range(N)]
ans = [1]*N
MOD = 10**9+7

for _ in range(M):
    a, b = map(int, input().split())
    
    if uf.is_same(a-1, b-1):
        continue
    
    if uf.count(a-1)>uf.count(b-1):
        a, b = b, a
    
    ra, rb = uf.root(a-1), uf.root(b-1)
    
    if uf.count(a-1)<uf.count(b-1):
        for v in belong[ra]:
            ans[v] = 0
    else:
        for v in belong[ra]:
            belong[rb].append(v)
        
        for v in belong[rb]:
            ans[v] = (ans[v]*2)%MOD
    
    uf.unite(a-1, b-1)
    
for i in range(N):
    print(pow(ans[i], MOD-2, MOD))
0