結果

問題 No.1451 集団登校
ユーザー mkawa2mkawa2
提出日時 2023-10-02 23:49:55
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 936 ms / 2,000 ms
コード長 2,465 bytes
コンパイル時間 118 ms
コンパイル使用メモリ 13,056 KB
実行使用メモリ 36,608 KB
最終ジャッジ日時 2024-07-26 13:56:49
合計ジャッジ時間 11,254 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 33 ms
11,008 KB
testcase_01 AC 33 ms
11,008 KB
testcase_02 AC 32 ms
11,008 KB
testcase_03 AC 33 ms
10,880 KB
testcase_04 AC 31 ms
11,008 KB
testcase_05 AC 33 ms
11,008 KB
testcase_06 AC 29 ms
11,008 KB
testcase_07 AC 32 ms
10,880 KB
testcase_08 AC 595 ms
29,696 KB
testcase_09 AC 31 ms
11,008 KB
testcase_10 AC 371 ms
27,136 KB
testcase_11 AC 236 ms
24,960 KB
testcase_12 AC 365 ms
26,112 KB
testcase_13 AC 672 ms
32,768 KB
testcase_14 AC 888 ms
35,968 KB
testcase_15 AC 410 ms
23,424 KB
testcase_16 AC 606 ms
30,080 KB
testcase_17 AC 482 ms
25,216 KB
testcase_18 AC 244 ms
18,816 KB
testcase_19 AC 155 ms
19,584 KB
testcase_20 AC 936 ms
36,608 KB
testcase_21 AC 318 ms
20,352 KB
testcase_22 AC 204 ms
16,640 KB
testcase_23 AC 103 ms
13,312 KB
testcase_24 AC 343 ms
26,496 KB
testcase_25 AC 402 ms
23,168 KB
testcase_26 AC 598 ms
29,952 KB
testcase_27 AC 663 ms
29,440 KB
testcase_28 AC 240 ms
17,792 KB
testcase_29 AC 437 ms
23,296 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

# sys.setrecursionlimit(200005)
# sys.set_int_max_str_digits(200005)
int1 = lambda x: int(x)-1
pDB = lambda *x: print(*x, end="\n", file=sys.stderr)
p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr)
def II(): return int(sys.stdin.readline())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
def SI(): return sys.stdin.readline().rstrip()

dij = [(0, 1), (-1, 0), (0, -1), (1, 0)]
# dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
# inf = -1-(-1 << 31)
inf = -1-(-1 << 63)
md = 10**9+7
# md = 998244353

class UnionFind:
    def __init__(self, n):
        self._tree = [-1]*n
        # number of connected component
        self.cnt = n
        self._top = list(range(n))

    def root(self, u):
        stack = []
        while self._tree[u] >= 0:
            stack.append(u)
            u = self._tree[u]
        for v in stack: self._tree[v] = u
        return u

    def same(self, u, v):
        return self.root(u) == self.root(v)

    def merge(self, u, v, i):
        u, v = self.root(u), self.root(v)
        self._top[u] = self._top[v] = i
        if u == v: return False
        if self._tree[u] > self._tree[v]: u, v = v, u
        self._tree[u] += self._tree[v]
        self._tree[v] = u
        self.cnt -= 1
        return True

    # size of connected component
    def size(self, u):
        return -self._tree[self.root(u)]

    def top(self, u):
        return self._top[self.root(u)]

n, m = LI()
uf = UnionFind(n)
dp = [0]*(n+m)
to = [[] for _ in range(n+m)]
for i in range(m):
    a, b = LI1()
    u, v = uf.top(a), uf.top(b)
    if u == v:
        to[i+n].append(u)
    else:
        to[i+n] += [u, v]
        su = uf.size(a)
        sv = uf.size(b)
        if su == sv: dp[i+n] = 1
        if su < sv: dp[u] = -1
        if su > sv: dp[v] = -1
    uf.merge(a, b, i+n)

fin = [0]*(n+m)
for u in range(n+m)[::-1]:
    if fin[u]: continue
    st = [u]
    while st:
        u = st.pop()
        fin[u] = 1
        for v in to[u]:
            if dp[u] == -1 or dp[v] == -1:
                dp[v] = -1
            else:
                dp[v] += dp[u]
            st.append(v)

inv = (md+1)//2
for d in dp[:n]:
    if d == -1:
        print(0)
    else:
        print(pow(inv, d, md))
0