結果
| 問題 |
No.2507 Yet Another Subgraph Counting
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-10-13 22:03:40 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 86 ms / 2,000 ms |
| コード長 | 5,020 bytes |
| コンパイル時間 | 1,337 ms |
| コンパイル使用メモリ | 117,988 KB |
| 実行使用メモリ | 5,760 KB |
| 最終ジャッジ日時 | 2024-09-15 17:45:11 |
| 合計ジャッジ時間 | 3,438 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 52 |
ソースコード
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using Int = long long;
template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")
// exp(as)
// assume as[0] == 0
// exp(a0 + a1 X) = exp(a0) + exp(a0) a1 X
// ZT1: T[2^(n-1)][n]
// ZT: T[2^n][n+1]
template <class T, class ZT1, class ZT>
vector<T> setExp(int n, const vector<T> &as, ZT1 zas, ZT zbs) {
assert(static_cast<int>(as.size()) == 1 << n);
assert(as[0] == 0);
zbs[0][0] = 1;
for (int m = 0; m < n; ++m) {
// ranked a[2^m, 2^(m+1))
for (int h = 0; h < 1 << m; ++h) {
memset(zas[h], 0, (m + 1) * sizeof(T));
zas[h][__builtin_popcount(h)] = as[(1 << m) + h];
}
// zeta
for (int w = 1; w < 1 << m; w <<= 1) {
for (int h0 = 0; h0 < 1 << m; h0 += w << 1) for (int h = h0; h < h0 + w; ++h) {
for (int k = 0; k <= m; ++k) zas[h + w][k] += zas[h][k];
}
}
for (int h = 0; h < 1 << m; ++h) {
// zeta
zbs[h][m + 1] = 0;
memcpy(zbs[(1 << m) + h], zbs[h], ((m + 1) + 1) * sizeof(T));
// product
for (int k = 0; k <= m; ++k) for (int l = 0; l <= m - k; ++l) {
zbs[(1 << m) + h][k + l + 1] += zbs[h][k] * zas[h][l];
}
}
}
// moebius
for (int w = 1; w < 1 << n; w <<= 1) {
for (int h0 = 0; h0 < 1 << n; h0 += w << 1) for (int h = h0; h < h0 + w; ++h) {
for (int k = 0; k <= n; ++k) zbs[h + w][k] -= zbs[h][k];
}
}
// unrank
vector<T> bs(1 << n);
for (int h = 0; h < 1 << n; ++h) bs[h] = zbs[h][__builtin_popcount(h)];
return bs;
}
using U = unsigned long long;
constexpr int MAX_N = 13;
int N, M;
vector<int> A, B;
U zas[1 << MAX_N][MAX_N + 1];
U zbs[1 << MAX_N][MAX_N + 1];
U sub[1 << MAX_N][MAX_N];
int main() {
for (; ~scanf("%d%d", &N, &M); ) {
A.resize(M);
B.resize(M);
for (int i = 0; i < M; ++i) {
scanf("%d%d", &A[i], &B[i]);
--A[i];
--B[i];
}
vector<int> adj(N, 0);
for (int i = 0; i < M; ++i) {
adj[A[i]] |= 1 << B[i];
adj[B[i]] |= 1 << A[i];
}
vector<U> cyc(1 << N, 0);
for (int s = 0; s < N; ++s) {
for (int p = 0; p < 1 << s; ++p) {
fill(sub[p], sub[p] + (s + 1), 0);
}
sub[0][s] = 1;
for (int p = 0; p < 1 << s; ++p) for (int u = 0; u <= s; ++u) if (sub[p][u]) {
for (int v = 0; v < s; ++v) if ((adj[u] >> v & 1) && !(p & 1 << v)) {
sub[p | 1 << v][v] += sub[p][u];
}
if (p != 1 << u && (adj[u] >> s & 1)) {
cyc[p | 1 << s] += sub[p][u];
}
}
cyc[1 << s] = 1;
for (int p = 1; p < 1 << s; ++p) if (cyc[p | 1 << s]) {
assert(__builtin_popcount(p) >= 2);
assert(cyc[p | 1 << s] % 2 == 0);
cyc[p | 1 << s] /= 2;
}
}
// cerr<<"cyc = "<<cyc<<endl;
vector<U> tree(1 << N, 0);
for (int s = 0; s < N; ++s) {
for (int p = 1 << s; p < 1 << (s + 1); ++p) if (cyc[p]) {
vector<int> us;
for (int u = 0; u < s; ++u) if (!(p & 1 << u)) {
us.push_back(u);
}
const int usLen = us.size();
vector<int> qs(1 << usLen, 0);
vector<int> ways(1 << usLen, 0);
for (int x = 0; x < usLen; ++x) {
const int deg = __builtin_popcount(adj[us[x]] & p);
for (int h = 0; h < 1 << x; ++h) {
qs[h | 1 << x] = qs[h] | 1 << us[x];
ways[h | 1 << x] = ways[h] + deg;
}
}
vector<U> fs(1 << usLen, 0);
for (int h = 0; h < 1 << usLen; ++h) {
fs[h] = ways[h] * tree[qs[h]];
}
const auto gs = setExp(usLen, fs, zas, zbs);
// cerr<<s<<" "<<p<<" "<<us<<" "<<fs<<" "<<gs<<endl;
for (int h = 0; h < 1 << usLen; ++h) {
tree[p | qs[h]] += cyc[p] * gs[h];
}
}
}
const auto ans = setExp(N, tree, zas, zbs);
// cerr<<"tree = "<<tree<<endl;
// cerr<<"ans = "<<ans<<endl;
printf("%llu\n", ans.back());
}
return 0;
}