結果

問題 No.2507 Yet Another Subgraph Counting
ユーザー rniyarniya
提出日時 2023-10-09 21:22:49
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 276 ms / 2,000 ms
コード長 7,493 bytes
コンパイル時間 2,654 ms
コンパイル使用メモリ 216,056 KB
実行使用メモリ 17,672 KB
最終ジャッジ日時 2023-10-13 18:27:39
合計ジャッジ時間 8,047 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 2 ms
4,352 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 10 ms
4,356 KB
testcase_06 AC 2 ms
4,352 KB
testcase_07 AC 1 ms
4,352 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 1 ms
4,348 KB
testcase_10 AC 2 ms
4,352 KB
testcase_11 AC 10 ms
4,372 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,352 KB
testcase_14 AC 10 ms
4,372 KB
testcase_15 AC 2 ms
4,356 KB
testcase_16 AC 2 ms
4,368 KB
testcase_17 AC 11 ms
4,348 KB
testcase_18 AC 2 ms
4,352 KB
testcase_19 AC 4 ms
4,348 KB
testcase_20 AC 2 ms
4,352 KB
testcase_21 AC 2 ms
4,352 KB
testcase_22 AC 2 ms
4,352 KB
testcase_23 AC 11 ms
4,428 KB
testcase_24 AC 3 ms
4,352 KB
testcase_25 AC 30 ms
5,564 KB
testcase_26 AC 90 ms
9,608 KB
testcase_27 AC 11 ms
4,508 KB
testcase_28 AC 31 ms
5,652 KB
testcase_29 AC 2 ms
4,352 KB
testcase_30 AC 91 ms
9,592 KB
testcase_31 AC 255 ms
17,460 KB
testcase_32 AC 255 ms
17,516 KB
testcase_33 AC 10 ms
4,352 KB
testcase_34 AC 4 ms
4,352 KB
testcase_35 AC 10 ms
4,352 KB
testcase_36 AC 3 ms
4,352 KB
testcase_37 AC 274 ms
17,512 KB
testcase_38 AC 272 ms
17,532 KB
testcase_39 AC 261 ms
17,572 KB
testcase_40 AC 276 ms
17,464 KB
testcase_41 AC 274 ms
17,536 KB
testcase_42 AC 272 ms
17,464 KB
testcase_43 AC 83 ms
9,592 KB
testcase_44 AC 253 ms
17,468 KB
testcase_45 AC 10 ms
4,432 KB
testcase_46 AC 10 ms
4,352 KB
testcase_47 AC 254 ms
17,672 KB
testcase_48 AC 3 ms
4,352 KB
testcase_49 AC 256 ms
17,460 KB
testcase_50 AC 5 ms
4,348 KB
testcase_51 AC 83 ms
10,040 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#ifdef LOCAL
#include <debug.hpp>
#else
#define debug(...) void(0)
#endif

namespace internal {

template <typename T, int LIM> std::vector<std::array<T, LIM + 1>> ranked_zeta(const std::vector<T>& a) {
    int n = a.size(), len = __builtin_ctz(n);
    assert((n & (n - 1)) == 0);
    std::vector<std::array<T, LIM + 1>> res(n);
    for (int i = 0; i < n; i++) res[i][__builtin_popcount(i)] = a[i];
    for (int step = 1; step < n; step <<= 1) {
        for (int start = 0; start < n; start += 2 * step) {
            for (int i = start; i < start + step; i++) {
                for (int j = 0; j <= len; j++) {
                    res[i | step][j] += res[i][j];
                }
            }
        }
    }
    return res;
}

template <typename T, int LIM> std::vector<T> ranked_mobius(std::vector<std::array<T, LIM + 1>>& ranked) {
    int n = ranked.size(), len = __builtin_ctz(n);
    assert((n & (n - 1)) == 0);
    for (int step = 1; step < n; step <<= 1) {
        for (int start = 0; start < n; start += 2 * step) {
            for (int i = start; i < start + step; i++) {
                for (int j = 0; j <= len; j++) {
                    ranked[i | step][j] -= ranked[i][j];
                }
            }
        }
    }
    std::vector<T> res(n);
    for (int i = 0; i < n; i++) res[i] = ranked[i][__builtin_popcount(i)];
    return res;
}

}  // namespace internal

template <typename T, int LIM = 20>
std::vector<T> subset_convolution(const std::vector<T>& a, const std::vector<T>& b) {
    auto ra = internal::ranked_zeta<T, LIM>(a);
    auto rb = internal::ranked_zeta<T, LIM>(b);
    int n = ra.size(), len = __builtin_ctz(n);
    for (int i = 0; i < n; i++) {
        auto &f = ra[i], &g = rb[i];
        for (int j = len; j >= 0; j--) {
            T sum = 0;
            for (int k = 0; k <= j; k++) sum += f[k] * g[j - k];
            f[j] = sum;
        }
    }
    return internal::ranked_mobius<T, LIM>(ra);
}

template <typename T, int LIM = 20> std::vector<T> exp_of_set_power_series(std::vector<T>& a) {
    int n = __builtin_ctz(a.size());
    assert(int(a.size()) == 1 << n and a[0] == T(0));
    std::vector<T> res(1 << n);
    res[0] = T(1);
    for (int i = 0; i < n; i++) {
        std::vector<T> f(begin(a) + (1 << i), begin(a) + (2 << i));
        std::vector<T> g(begin(res), begin(res) + (1 << i));
        auto h = subset_convolution<T, LIM>(f, g);
        std::copy(begin(h), end(h), begin(res) + (1 << i));
    }
    return res;
}

using namespace std;

typedef long long ll;
#define all(x) begin(x), end(x)
constexpr int INF = (1 << 30) - 1;
constexpr long long IINF = (1LL << 60) - 1;
constexpr int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

template <class T> istream& operator>>(istream& is, vector<T>& v) {
    for (auto& x : v) is >> x;
    return is;
}

template <class T> ostream& operator<<(ostream& os, const vector<T>& v) {
    auto sep = "";
    for (const auto& x : v) os << exchange(sep, " ") << x;
    return os;
}

template <class T, class U = T> bool chmin(T& x, U&& y) { return y < x and (x = forward<U>(y), true); }

template <class T, class U = T> bool chmax(T& x, U&& y) { return x < y and (x = forward<U>(y), true); }

template <class T> void mkuni(vector<T>& v) {
    sort(begin(v), end(v));
    v.erase(unique(begin(v), end(v)), end(v));
}

template <class T> int lwb(const vector<T>& v, const T& x) { return lower_bound(begin(v), end(v), x) - begin(v); }

int topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); }
int topbit(long long t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); }
int botbit(signed a) { return a == 0 ? 32 : __builtin_ctz(a); }
int botbit(long long a) { return a == 0 ? 64 : __builtin_ctzll(a); }
int popcount(signed t) { return __builtin_popcount(t); }
int popcount(long long t) { return __builtin_popcountll(t); }
bool ispow2(int i) { return i && (i & -i) == i; }
long long MSK(int n) { return (1LL << n) - 1; }

/*
辺部分集合であって、閉路に含まれる頂点集合が互いに非交差である
連結成分ごと
f(S) = 頂点集合 S を連結とし、閉路に含まれる頂点が非交差であるような、S 内の辺部分集合の総数
exp f (V) が答え
f(S) は?
閉路を 1 つの連結成分に潰す
閉路内ではその外周以外に辺が含まれない(閉路が 2 つ以上できるから)
閉路を潰すと木になる
c(S) = S を閉路とするような辺部分集合の総数
bit dp で計算
*/

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, m;
    cin >> n >> m;
    vector<int> G(n, 0);
    for (; m--;) {
        int u, v;
        cin >> u >> v;
        u--, v--;
        G[u] |= 1 << v;
        G[v] |= 1 << u;
    }

    vector<ll> c(1 << n, 0);  // S を閉路とする S 内の辺部分集合の総数
    {
        vector dp(1 << n, vector(n, vector<ll>(n, 0)));
        for (int i = 0; i < n; i++) {
            dp[1 << i][i][i] = 1;
            c[1 << i] += 1;
        }
        for (int mask = 0; mask < 1 << n; mask++) {
            for (int start = 0; start < n; start++) {
                for (int cur = 0; cur < n; cur++) {
                    ll& val = dp[mask][start][cur];
                    if (val == 0) continue;
                    for (int nxt = 0; nxt < n; nxt++) {
                        if (mask >> nxt & 1) continue;
                        if (~G[cur] >> nxt & 1) continue;
                        dp[mask | 1 << nxt][start][nxt] += val;
                    }
                    if (__builtin_popcount(mask) > 2 and (G[cur] >> start & 1)) c[mask] += val;
                }
            }
        }
        for (int mask = 0; mask < 1 << n; mask++) {
            if (popcount(mask) == 1) continue;
            ll& val = c[mask];
            if (val == 0) continue;
            val /= popcount(mask) * 2;
        }
    }

    vector<ll> f(1 << n);  // S を連結とし、閉路内の頂点が互いに非交差であるような S 内の辺部分集合の総数
    f[0] = 0;
    for (int C = 1; C < 1 << n; C++) {
        /*
        p を含む閉路 C を固定
        {p 未満の頂点} \ C の連結成分 T_1, ... , T_k
        C から T_i への辺の総数を重みづけて subset convolution
        計算量は?
        sum_C (p - (|C| - 1))^2 2^{p - (|C| - 1)}
        <= n^2 sum_{p = 0}^{n - 1} sum_{i = 0}^p binom(p, i) 2^{p - i + 1}
        =  n^2 sum_{p = 0}^{n - 1} 2 * 3^p
        O(n^2 3^n)
        */
        int p = topbit(C);
        vector<int> rest;
        for (int i = 0; i < p; i++) {
            if (~C >> i & 1) {
                rest.emplace_back(i);
            }
        }
        auto convert = [&](int mask) {
            int res = 0;
            for (int i = 0; i < int(rest.size()); i++) {
                if (mask >> i & 1) {
                    res |= 1 << rest[i];
                }
            }
            return res;
        };
        int len = rest.size();
        vector<ll> g(1 << len);
        for (int T = 0; T < 1 << len; T++) {
            int U = convert(T), es = 0;
            for (int i = 0; i < p; i++) {
                if (U >> i & 1) {
                    es += popcount(G[i] & C);
                }
            }
            g[T] = f[U] * es;
        }
        g = exp_of_set_power_series(g);
        for (int T = 0; T < 1 << len; T++) f[convert(T) | C] += g[T] * c[C];
    }

    f = exp_of_set_power_series(f);
    ll ans = f.back();

    cout << ans << '\n';
    return 0;
}
0