結果

問題 No.2507 Yet Another Subgraph Counting
ユーザー suisensuisen
提出日時 2023-08-21 22:49:00
言語 PyPy3
(7.3.15)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 4,575 bytes
コンパイル時間 386 ms
コンパイル使用メモリ 87,008 KB
実行使用メモリ 130,624 KB
最終ジャッジ日時 2023-10-13 18:25:59
合計ジャッジ時間 37,056 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 191 ms
82,344 KB
testcase_01 AC 187 ms
82,432 KB
testcase_02 AC 186 ms
82,352 KB
testcase_03 AC 171 ms
81,788 KB
testcase_04 AC 169 ms
81,108 KB
testcase_05 AC 365 ms
85,152 KB
testcase_06 AC 170 ms
81,076 KB
testcase_07 AC 186 ms
82,572 KB
testcase_08 AC 219 ms
82,540 KB
testcase_09 AC 173 ms
80,940 KB
testcase_10 AC 215 ms
82,748 KB
testcase_11 AC 361 ms
84,996 KB
testcase_12 AC 218 ms
82,872 KB
testcase_13 AC 221 ms
82,924 KB
testcase_14 AC 366 ms
85,200 KB
testcase_15 AC 173 ms
81,104 KB
testcase_16 AC 175 ms
80,928 KB
testcase_17 AC 365 ms
84,848 KB
testcase_18 AC 172 ms
80,976 KB
testcase_19 AC 324 ms
83,980 KB
testcase_20 AC 172 ms
80,916 KB
testcase_21 AC 187 ms
82,420 KB
testcase_22 AC 223 ms
82,836 KB
testcase_23 AC 379 ms
84,708 KB
testcase_24 AC 278 ms
83,860 KB
testcase_25 AC 486 ms
87,356 KB
testcase_26 AC 823 ms
95,068 KB
testcase_27 AC 368 ms
84,620 KB
testcase_28 AC 478 ms
87,448 KB
testcase_29 AC 221 ms
82,920 KB
testcase_30 AC 824 ms
95,544 KB
testcase_31 AC 1,956 ms
127,420 KB
testcase_32 AC 1,927 ms
127,912 KB
testcase_33 AC 361 ms
84,496 KB
testcase_34 AC 313 ms
83,848 KB
testcase_35 AC 357 ms
85,000 KB
testcase_36 AC 263 ms
83,464 KB
testcase_37 AC 1,935 ms
124,864 KB
testcase_38 TLE -
testcase_39 AC 1,922 ms
123,716 KB
testcase_40 AC 1,946 ms
124,620 KB
testcase_41 AC 1,960 ms
124,444 KB
testcase_42 AC 1,968 ms
124,528 KB
testcase_43 AC 803 ms
91,672 KB
testcase_44 AC 1,948 ms
127,128 KB
testcase_45 AC 355 ms
84,776 KB
testcase_46 AC 356 ms
85,260 KB
testcase_47 AC 1,933 ms
126,792 KB
testcase_48 AC 263 ms
83,140 KB
testcase_49 AC 1,944 ms
130,624 KB
testcase_50 AC 312 ms
83,784 KB
testcase_51 AC 810 ms
92,288 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 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 exp(f: List[int], n: int):
    """
    Subset exp of Σ[S⊆{0,1,...,n-1}] f(S)
    """
    assert f[0] == 0
    rf = ranked([1], 0)
    for i in range(n):
        rg = ranked(f[1 << i: 1 << (i + 1)], i)
        subset_zeta_poly(rg, i)
        for S in range(1 << i):
            rf[S].append(0)
            rg[S].insert(0, 1)
            rh = mul_poly(rf[S], rg[S])
            rf.append(rh)
    subset_mobius_poly(rf, n)
    return deranked(rf, n)

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

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

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 = count_cycles(n, edges)

f = [0] * (1 << n)
for C in range(1, 1 << n):
    # max C
    t = C.bit_length() - 1
    # {0, ..., tX} - C
    S = ((1 << (t + 1)) - 1) ^ C
    k = popcount[S]

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

    for A, hA in enumerate(exp(g, k)):
        X = bit_deposit(A, S, t) | C
        f[X] += cycle[C] * hA

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