結果
問題 | No.2507 Yet Another Subgraph Counting |
ユーザー | suisen |
提出日時 | 2023-08-31 22:08:43 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,406 ms / 2,000 ms |
コード長 | 4,565 bytes |
コンパイル時間 | 260 ms |
コンパイル使用メモリ | 81,920 KB |
実行使用メモリ | 114,404 KB |
最終ジャッジ日時 | 2024-09-15 14:17:58 |
合計ジャッジ時間 | 13,992 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge6 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 74 ms
73,216 KB |
testcase_01 | AC | 74 ms
72,960 KB |
testcase_02 | AC | 74 ms
72,192 KB |
testcase_03 | AC | 70 ms
69,760 KB |
testcase_04 | AC | 67 ms
68,864 KB |
testcase_05 | AC | 130 ms
79,104 KB |
testcase_06 | AC | 65 ms
68,608 KB |
testcase_07 | AC | 71 ms
72,064 KB |
testcase_08 | AC | 78 ms
74,932 KB |
testcase_09 | AC | 65 ms
68,992 KB |
testcase_10 | AC | 78 ms
74,496 KB |
testcase_11 | AC | 134 ms
79,128 KB |
testcase_12 | AC | 77 ms
75,008 KB |
testcase_13 | AC | 86 ms
77,696 KB |
testcase_14 | AC | 152 ms
79,860 KB |
testcase_15 | AC | 65 ms
68,864 KB |
testcase_16 | AC | 64 ms
68,480 KB |
testcase_17 | AC | 144 ms
79,452 KB |
testcase_18 | AC | 65 ms
68,864 KB |
testcase_19 | AC | 112 ms
78,592 KB |
testcase_20 | AC | 65 ms
68,992 KB |
testcase_21 | AC | 71 ms
71,552 KB |
testcase_22 | AC | 95 ms
78,324 KB |
testcase_23 | AC | 227 ms
80,532 KB |
testcase_24 | AC | 147 ms
79,312 KB |
testcase_25 | AC | 236 ms
81,460 KB |
testcase_26 | AC | 413 ms
83,844 KB |
testcase_27 | AC | 209 ms
79,924 KB |
testcase_28 | AC | 238 ms
80,824 KB |
testcase_29 | AC | 101 ms
78,592 KB |
testcase_30 | AC | 421 ms
85,496 KB |
testcase_31 | AC | 199 ms
87,604 KB |
testcase_32 | AC | 206 ms
87,488 KB |
testcase_33 | AC | 147 ms
79,744 KB |
testcase_34 | AC | 112 ms
78,584 KB |
testcase_35 | AC | 124 ms
79,360 KB |
testcase_36 | AC | 96 ms
78,592 KB |
testcase_37 | AC | 666 ms
94,292 KB |
testcase_38 | AC | 1,284 ms
113,648 KB |
testcase_39 | AC | 302 ms
88,276 KB |
testcase_40 | AC | 806 ms
96,852 KB |
testcase_41 | AC | 1,227 ms
108,156 KB |
testcase_42 | AC | 1,406 ms
114,404 KB |
testcase_43 | AC | 163 ms
82,292 KB |
testcase_44 | AC | 195 ms
87,960 KB |
testcase_45 | AC | 128 ms
79,104 KB |
testcase_46 | AC | 130 ms
78,976 KB |
testcase_47 | AC | 191 ms
87,216 KB |
testcase_48 | AC | 99 ms
78,444 KB |
testcase_49 | AC | 258 ms
89,004 KB |
testcase_50 | AC | 112 ms
79,488 KB |
testcase_51 | AC | 164 ms
82,096 KB |
ソースコード
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 add_rank(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 remove_rank(rf: List[List[int]], n: int): """ Remove rank """ return [rf[S][popcount[S]] for S in range(1 << n)] def subset_exp(f: List[int], n: int): """ Subset exp of Σ[S⊆{0,1,...,n-1}] f(S) x^S """ assert f[0] == 0 rf = add_rank([1], 0) for i in range(n): rg = add_rank(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 remove_rank(rf, n) 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): 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 = [f[bit_deposit[A]] * (E[bit_deposit[A] | C] - E[bit_deposit[A]] - E[C]) for A in range(1 << k)] for A, hA in enumerate(subset_exp(g, k)): f[bit_deposit[A] | C] += cycle[C] * hA print(subset_exp(f, n)[-1])