結果

問題 No.1451 集団登校
ユーザー NatsubiSoganNatsubiSogan
提出日時 2021-04-11 11:57:54
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 777 ms / 2,000 ms
コード長 1,603 bytes
コンパイル時間 165 ms
コンパイル使用メモリ 10,892 KB
実行使用メモリ 29,512 KB
最終ジャッジ日時 2023-09-09 14:39:38
合計ジャッジ時間 10,353 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
8,320 KB
testcase_01 AC 16 ms
8,448 KB
testcase_02 AC 16 ms
8,360 KB
testcase_03 AC 16 ms
8,088 KB
testcase_04 AC 16 ms
8,324 KB
testcase_05 AC 16 ms
8,324 KB
testcase_06 AC 16 ms
8,352 KB
testcase_07 AC 16 ms
8,412 KB
testcase_08 AC 777 ms
29,512 KB
testcase_09 AC 16 ms
8,276 KB
testcase_10 AC 242 ms
23,224 KB
testcase_11 AC 117 ms
21,880 KB
testcase_12 AC 245 ms
21,776 KB
testcase_13 AC 496 ms
24,712 KB
testcase_14 AC 673 ms
21,736 KB
testcase_15 AC 302 ms
17,348 KB
testcase_16 AC 448 ms
21,876 KB
testcase_17 AC 316 ms
9,220 KB
testcase_18 AC 180 ms
14,148 KB
testcase_19 AC 74 ms
16,484 KB
testcase_20 AC 690 ms
22,260 KB
testcase_21 AC 216 ms
8,744 KB
testcase_22 AC 146 ms
10,368 KB
testcase_23 AC 66 ms
8,044 KB
testcase_24 AC 224 ms
22,640 KB
testcase_25 AC 306 ms
16,468 KB
testcase_26 AC 443 ms
22,036 KB
testcase_27 AC 474 ms
15,208 KB
testcase_28 AC 178 ms
11,788 KB
testcase_29 AC 324 ms
16,024 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