結果

問題 No.1451 集団登校
ユーザー NatsubiSoganNatsubiSogan
提出日時 2021-04-11 11:58:10
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 699 ms / 2,000 ms
コード長 1,603 bytes
コンパイル時間 364 ms
コンパイル使用メモリ 86,864 KB
実行使用メモリ 97,360 KB
最終ジャッジ日時 2023-09-09 14:39:49
合計ジャッジ時間 10,057 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 66 ms
71,180 KB
testcase_01 AC 71 ms
71,236 KB
testcase_02 AC 65 ms
71,204 KB
testcase_03 AC 66 ms
70,948 KB
testcase_04 AC 67 ms
71,160 KB
testcase_05 AC 67 ms
71,208 KB
testcase_06 AC 65 ms
71,108 KB
testcase_07 AC 66 ms
71,144 KB
testcase_08 AC 168 ms
95,328 KB
testcase_09 AC 64 ms
71,280 KB
testcase_10 AC 247 ms
90,076 KB
testcase_11 AC 106 ms
87,240 KB
testcase_12 AC 334 ms
93,924 KB
testcase_13 AC 453 ms
93,624 KB
testcase_14 AC 359 ms
88,820 KB
testcase_15 AC 525 ms
95,568 KB
testcase_16 AC 538 ms
94,248 KB
testcase_17 AC 234 ms
79,944 KB
testcase_18 AC 353 ms
87,416 KB
testcase_19 AC 96 ms
83,456 KB
testcase_20 AC 699 ms
97,360 KB
testcase_21 AC 184 ms
79,340 KB
testcase_22 AC 278 ms
83,416 KB
testcase_23 AC 117 ms
78,576 KB
testcase_24 AC 259 ms
90,096 KB
testcase_25 AC 464 ms
90,228 KB
testcase_26 AC 386 ms
92,344 KB
testcase_27 AC 562 ms
91,300 KB
testcase_28 AC 354 ms
85,264 KB
testcase_29 AC 514 ms
91,008 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.par = [-1] * self.n
    def find(self, x):
        r = x
        while not self.par[r] < 0:
            r = self.par[r]
        t = x
        while t != r:
            m = t
            t = self.par[t]
            self.par[m] = r
        return r

    def unite(self, x, y):
        p = self.find(x)
        q = self.find(y)
        if p == q:
            return None
        if self.par[p] >= self.par[q]:
            p, q = q, p
        self.par[p] += self.par[q]
        self.par[q] = p

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def size(self, x):
        return -self.par[self.find(x)]


#拡張Euclidの互除法
def extgcd(a, b, d = 0):
    g = a
    if b == 0:
        x, y = 1, 0
    else:
        x, y, g = extgcd(b, a % b)
        x, y = y, x - a // b * y
    return x, y, g

#mod p における逆元
def invmod(a, p):
    x, y, g = extgcd(a, p)
    x %= p
    return x

mod = 10 ** 9 + 7
n, m = map(int, input().split())
UF = UnionFind(n)
g = [[i] for i in range(n)]
ans = [1] * n
inv2 = invmod(2, mod)

for _ in range(m):
    a, b = map(int, input().split())
    a -= 1; b -= 1
    if UF.same(a, b):
        continue
    if UF.size(a) > UF.size(b):
        a, b = b, a
    pa, pb = UF.find(a), UF.find(b)
    if UF.size(a) < UF.size(b):
        for i in g[pa]:
            ans[i] = 0
    else:
        for i in g[pa]:
            g[pb].append(i)
        
        for i in g[pb]:
            ans[i] *= inv2
            ans[i] %= mod
    UF.unite(a, b)
    
for i in ans: print(i)
0