結果

問題 No.2507 Yet Another Subgraph Counting
ユーザー rniyarniya
提出日時 2023-10-09 21:32:50
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 262 ms / 2,000 ms
コード長 6,391 bytes
コンパイル時間 2,663 ms
コンパイル使用メモリ 216,784 KB
実行使用メモリ 17,664 KB
最終ジャッジ日時 2024-09-15 14:18:35
合計ジャッジ時間 7,220 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 9 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 3 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 10 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 3 ms
5,376 KB
testcase_14 AC 10 ms
5,376 KB
testcase_15 AC 1 ms
5,376 KB
testcase_16 AC 1 ms
5,376 KB
testcase_17 AC 10 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 5 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 11 ms
5,376 KB
testcase_24 AC 3 ms
5,376 KB
testcase_25 AC 29 ms
5,760 KB
testcase_26 AC 86 ms
9,856 KB
testcase_27 AC 11 ms
5,376 KB
testcase_28 AC 28 ms
5,760 KB
testcase_29 AC 2 ms
5,376 KB
testcase_30 AC 85 ms
9,984 KB
testcase_31 AC 240 ms
17,536 KB
testcase_32 AC 238 ms
17,536 KB
testcase_33 AC 10 ms
5,376 KB
testcase_34 AC 4 ms
5,376 KB
testcase_35 AC 10 ms
5,376 KB
testcase_36 AC 3 ms
5,376 KB
testcase_37 AC 256 ms
17,536 KB
testcase_38 AC 252 ms
17,536 KB
testcase_39 AC 243 ms
17,664 KB
testcase_40 AC 261 ms
17,664 KB
testcase_41 AC 262 ms
17,536 KB
testcase_42 AC 261 ms
17,536 KB
testcase_43 AC 78 ms
9,984 KB
testcase_44 AC 244 ms
17,536 KB
testcase_45 AC 10 ms
5,376 KB
testcase_46 AC 10 ms
5,376 KB
testcase_47 AC 243 ms
17,664 KB
testcase_48 AC 3 ms
5,376 KB
testcase_49 AC 242 ms
17,536 KB
testcase_50 AC 4 ms
5,376 KB
testcase_51 AC 79 ms
9,856 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; }

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);
    {
        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);
    f[0] = 0;
    for (int C = 1; C < 1 << n; C++) {
        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<ll, 13>(g);
        for (int T = 0; T < 1 << len; T++) f[convert(T) | C] += g[T] * c[C];
    }

    f = exp_of_set_power_series<ll, 13>(f);
    ll ans = f.back();

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