結果

問題 No.2507 Yet Another Subgraph Counting
ユーザー suisensuisen
提出日時 2023-08-31 22:00:19
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,214 ms / 2,000 ms
コード長 4,503 bytes
コンパイル時間 455 ms
コンパイル使用メモリ 87,032 KB
実行使用メモリ 98,760 KB
最終ジャッジ日時 2023-10-13 18:27:09
合計ジャッジ時間 16,548 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 173 ms
82,236 KB
testcase_01 AC 163 ms
82,384 KB
testcase_02 AC 163 ms
82,184 KB
testcase_03 AC 156 ms
81,164 KB
testcase_04 AC 150 ms
80,976 KB
testcase_05 AC 195 ms
83,292 KB
testcase_06 AC 179 ms
80,936 KB
testcase_07 AC 166 ms
82,284 KB
testcase_08 AC 165 ms
82,308 KB
testcase_09 AC 148 ms
81,080 KB
testcase_10 AC 160 ms
82,308 KB
testcase_11 AC 192 ms
83,024 KB
testcase_12 AC 164 ms
82,256 KB
testcase_13 AC 174 ms
82,436 KB
testcase_14 AC 239 ms
83,544 KB
testcase_15 AC 155 ms
81,032 KB
testcase_16 AC 150 ms
81,048 KB
testcase_17 AC 212 ms
83,020 KB
testcase_18 AC 178 ms
80,900 KB
testcase_19 AC 173 ms
82,500 KB
testcase_20 AC 145 ms
81,060 KB
testcase_21 AC 158 ms
82,176 KB
testcase_22 AC 179 ms
82,660 KB
testcase_23 AC 269 ms
83,816 KB
testcase_24 AC 215 ms
82,824 KB
testcase_25 AC 294 ms
84,408 KB
testcase_26 AC 373 ms
86,020 KB
testcase_27 AC 284 ms
84,016 KB
testcase_28 AC 283 ms
84,632 KB
testcase_29 AC 179 ms
82,316 KB
testcase_30 AC 386 ms
86,744 KB
testcase_31 AC 233 ms
85,204 KB
testcase_32 AC 231 ms
84,008 KB
testcase_33 AC 219 ms
83,292 KB
testcase_34 AC 180 ms
82,736 KB
testcase_35 AC 189 ms
83,252 KB
testcase_36 AC 174 ms
82,424 KB
testcase_37 AC 579 ms
91,392 KB
testcase_38 AC 1,214 ms
98,760 KB
testcase_39 AC 334 ms
86,304 KB
testcase_40 AC 794 ms
90,468 KB
testcase_41 AC 867 ms
93,604 KB
testcase_42 AC 917 ms
93,572 KB
testcase_43 AC 212 ms
84,084 KB
testcase_44 AC 247 ms
85,640 KB
testcase_45 AC 223 ms
83,040 KB
testcase_46 AC 201 ms
83,288 KB
testcase_47 AC 236 ms
86,292 KB
testcase_48 AC 178 ms
82,484 KB
testcase_49 AC 288 ms
86,780 KB
testcase_50 AC 199 ms
83,164 KB
testcase_51 AC 265 ms
84,276 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from typing import List, Tuple

N_MAX = 13
popcount = [0] * (1 << N_MAX)
for S in range(1, 1 << N_MAX):
    popcount[S] = popcount[S & (S - 1)] + 1

def subset_zeta(f: List[int]):
    """
    Inplace conversion from f to ζf. ζf is defined as follows:
        (ζf)(S) = Σ[T⊆S] f(T)
    """
    n = len(f)
    block = 1
    while block < n:
        offset = 0
        while offset < n:
            for p in range(offset, offset + block):
                f[p + block] += f[p]
            offset += 2 * block
        block <<= 1

def ranked_zeta(f: List[List[int]]):
    """
    Inplace conversion from f to ζf. ζf is defined as follows:
        (ζf)(S) = Σ[T⊆S] f(T)
    """
    n = len(f)
    block = 1
    while block < n:
        offset = 0
        while offset < n:
            for p in range(offset, offset + block):
                a = f[p + block]
                b = f[p]
                for i in range(N_MAX + 1):
                    a[i] += b[i]
            offset += 2 * block
        block <<= 1

def ranked_mobius(f: List[List[int]]):
    """
    Inplace conversion from f to μf. μf is defined as follows:
        (μf)(S) = Σ[T⊆S] (-1)^(|S/T|) f(T)
    """
    n = len(f)
    block = 1
    while block < n:
        offset = 0
        while offset < n:
            for p in range(offset, offset + block):
                a = f[p + block]
                b = f[p]
                for i in range(N_MAX + 1):
                    a[i] -= b[i]
            offset += 2 * block
        block <<= 1

def add_rank(f: List[int]):
    """
    Add rank
    """
    return [[(i == popcount[S]) * f[S] for i in range(N_MAX + 1)] for S in range(len(f))]

def remove_rank(rf: List[List[int]]):
    """
    Remove rank
    """
    return [rf[S][popcount[S]] for S in range(len(rf))]

def subset_exp(f: List[int]):
    """
    Subset subset_exp of Σ[S⊆{0,1,...,n-1}] f(S)
    """
    assert f[0] == 0

    n = 0
    while 1 << n != len(f):
        n += 1

    rf = add_rank([1])
    for i in range(n):
        rg = add_rank(f[1 << i: 1 << (i + 1)])
        ranked_zeta(rg)
        for S in range(1 << i):
            rf[S].append(0)
            rg[S].insert(0, 1)
            a = rf[S]
            b = rg[S]
            for k in reversed(range(i + 2)):
                v = 0
                for x in range(k + 1):
                    v += a[k - x] * b[x]
                b[k] = v
            rf.append(b)
    ranked_mobius(rf)
    return remove_rank(rf)


def count_cycles(n: int, edges: List[Tuple[int, int]]):
    cycle = [0] * (1 << n)
    adj = [[] for _ in range(n)]
    for u, v in edges:
        adj[u].append(v)
        adj[v].append(u)

    cycle_dp = [[0] * n for _ in range(1 << n)]
    for v in range(n):
        cycle_dp[1 << v][v] = 1
    for s in range(1, 1 << n):
        start = 0
        while not ((s >> start) & 1):
            start += 1
        for cur in range(n):
            if cycle_dp[s][cur] == 0:
                continue
            for nxt in adj[cur]:
                if start == nxt:
                    cycle[s] += cycle_dp[s][cur]
                elif start < nxt and not ((s >> nxt) & 1):
                    cycle_dp[s | (1 << nxt)][nxt] += cycle_dp[s][cur]

    for s in range(1, 1 << n):
        if popcount[s] == 1:
            cycle[s] = 1
        elif popcount[s] == 2:
            cycle[s] = 0
        else:
            cycle[s] //= 2
    return cycle

if __name__ == '__main__':
    n, m = map(int, input().split())

    edges = []
    for _ in range(m):
        u, v = map(int, input().split())
        u -= 1
        v -= 1
        edges.append((u, v))

    # E[S] = # of edges connecting vertices in S
    E = [0] * (1 << n)
    for u, v in edges:
        E[(1 << u) | (1 << v)] += 1
    subset_zeta(E)

    cycle = count_cycles(n, edges)

    f = [0] * (1 << n)
    for C in range(1, 1 << n):
        if cycle[C] == 0:
            continue

        # max C
        t = C.bit_length() - 1
        # {0, ..., tX} - C
        S = ((1 << (t + 1)) - 1) ^ C
        k = popcount[S]

        bit_deposit = [0] * (1 << k)
        bit_deposit[0] = S
        for A in range(1, 1 << k):
            bit_deposit[A] = (bit_deposit[A - 1] - 1) & S
        bit_deposit.reverse()

        g = [0] * (1 << k)
        for A in range(1 << k):
            g[A] = f[bit_deposit[A]] * (E[bit_deposit[A] | C] - E[bit_deposit[A]] - E[C])

        for A, hA in enumerate(subset_exp(g)):
            f[bit_deposit[A] | C] += cycle[C] * hA

    print(subset_exp(f)[-1])
0