結果

問題 No.2507 Yet Another Subgraph Counting
ユーザー suisensuisen
提出日時 2023-08-21 04:38:39
言語 PyPy3
(7.3.15)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 4,597 bytes
コンパイル時間 309 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 111,712 KB
最終ジャッジ日時 2024-06-11 01:40:21
合計ジャッジ時間 31,968 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 96 ms
78,592 KB
testcase_01 AC 104 ms
78,592 KB
testcase_02 AC 99 ms
78,336 KB
testcase_03 AC 79 ms
72,192 KB
testcase_04 AC 73 ms
69,632 KB
testcase_05 AC 281 ms
81,760 KB
testcase_06 AC 75 ms
68,864 KB
testcase_07 AC 96 ms
78,464 KB
testcase_08 AC 124 ms
79,104 KB
testcase_09 AC 70 ms
69,504 KB
testcase_10 AC 123 ms
79,360 KB
testcase_11 AC 279 ms
81,696 KB
testcase_12 AC 125 ms
79,104 KB
testcase_13 AC 141 ms
79,104 KB
testcase_14 AC 310 ms
81,636 KB
testcase_15 AC 78 ms
69,120 KB
testcase_16 AC 78 ms
69,760 KB
testcase_17 AC 314 ms
81,464 KB
testcase_18 AC 69 ms
69,504 KB
testcase_19 AC 227 ms
81,964 KB
testcase_20 AC 68 ms
69,888 KB
testcase_21 AC 96 ms
78,592 KB
testcase_22 AC 130 ms
79,616 KB
testcase_23 AC 288 ms
81,780 KB
testcase_24 AC 189 ms
80,304 KB
testcase_25 AC 382 ms
82,868 KB
testcase_26 AC 735 ms
87,504 KB
testcase_27 AC 321 ms
82,376 KB
testcase_28 AC 408 ms
82,612 KB
testcase_29 AC 126 ms
79,616 KB
testcase_30 AC 720 ms
86,848 KB
testcase_31 AC 1,763 ms
110,776 KB
testcase_32 TLE -
testcase_33 AC 290 ms
81,720 KB
testcase_34 AC 258 ms
81,892 KB
testcase_35 AC 282 ms
81,896 KB
testcase_36 AC 166 ms
80,176 KB
testcase_37 AC 1,761 ms
111,712 KB
testcase_38 AC 1,824 ms
110,952 KB
testcase_39 AC 1,793 ms
110,440 KB
testcase_40 AC 1,696 ms
111,080 KB
testcase_41 AC 1,840 ms
111,320 KB
testcase_42 AC 1,765 ms
110,940 KB
testcase_43 AC 708 ms
87,328 KB
testcase_44 AC 1,796 ms
110,396 KB
testcase_45 AC 313 ms
81,572 KB
testcase_46 AC 270 ms
81,832 KB
testcase_47 AC 1,746 ms
110,648 KB
testcase_48 AC 164 ms
80,168 KB
testcase_49 AC 1,717 ms
110,604 KB
testcase_50 AC 260 ms
81,916 KB
testcase_51 AC 726 ms
87,336 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from typing import List

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

def addeq_poly(f: List[int], g: List[int]):
    """
    f += g
    """
    for i, gi in enumerate(g):
        f[i] += gi

def subeq_poly(f: List[int], g: List[int]):
    """
    f -= g
    """
    for i, gi in enumerate(g):
        f[i] -= gi

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

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

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

def mul_poly(f: List[int], g: List[int]):
    """
    Returns h = fg mod x^n, where f, g are polynomials with degree n-1 defined as follows:
        f(x) = Σ_i f[i] x^i,
        g(x) = Σ_i g[i] x^i.
    """
    n = len(f)
    h = [0] * n
    for i in range(n):
        for j in range(n - i):
            h[i + j] += f[i] * g[j]

    return h

def ranked(f: List[int], n: int):
    """
    Add rank
    """
    return [[(i == popcount[S]) * f[S] for i in range(n + 1)] for S in range(1 << n)]

def deranked(rf: List[List[int]], n: int):
    """
    Remove rank
    """
    return [rf[S][popcount[S]] for S in range(1 << n)]

def subset_conv(f: List[int], g: List[int], n: int):
    rf = ranked(f, n)
    rg = ranked(g, n)
    subset_zeta_poly(rf, n)
    subset_zeta_poly(rg, n)
    for i in range(1 << n):
        rf[i] = mul_poly(rf[i], rg[i])
    subset_mobius_poly(rf, n)
    return deranked(rf, n)

def exp(f: List[int], n: int):
    """
    Subset exp of Σ[S⊆{0,1,...,n-1}] f(S)
    """
    assert f[0] == 0
    g = [1]
    for i in range(n):
        g += subset_conv(g, f[1 << i: 1 << (i + 1)], i)
    return g

def bit_deposit(src, mask):
    dst = 0
    j = 0
    for i in range(N_MAX):
        if (mask >> i) & 1:
            dst |= ((src >> j) & 1) << i
            j += 1
    return dst

def bit_extract(src, mask):
    dst = 0
    j = 0
    for i in range(N_MAX):
        if (mask >> i) & 1:
            dst |= ((src >> i) & 1) << j
            j += 1
    return dst

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, n)

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

f = [0]
for v in range(n):
    f += [0] * (1 << v)
    for X in range(1 << v, 1 << (v + 1)):
        mask = ((1 << (v + 1)) - 1) ^ X
        k = popcount[mask]

        g = [0] * (1 << k)
        for T in range(1 << k):
            S = bit_deposit(T, mask)
            g[T] = f[S] * (E[S | X] - E[S] - E[X])

        exp_g = exp(g, k)
        for T in range(1 << k):
            S = bit_deposit(T, mask)
            f[S | X] += cycle[X] * exp_g[T]

print(exp(f, n)[-1])
0