competitive-programing

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub Astral-23/competitive-programing

:heavy_check_mark: Graph/Chromatic_Number_with_restoration.hpp

Depends on

Verified with

Code

#include "../Convolution/bitwise_or_convolution.hpp"

using S = ll;
S op(S a, S b) {
    return a + b;
}
S inv(S x) {
    return -x;
}
S zero() {
    return 0;
}

pair<int, vec<int>> chromatic_number(const vec<vec<int>> &g) {
    if (g.empty()) return {0, {}};
    int n = g.size();

    vec<vec<ll>> dps(1, vec<ll>(1 << n, 0));
    dps[0][0] = 1;

    vec<ll> dp(1 << n, -1);  // dp[i][S] := S は i 色彩色可能か?
    dp[0] = 1;
    rep(i, 0, n) dp[1 << i] = 1;

    rep(s, 0, 1 << n) if (dp[s] == -1) {
        int lat = -1;
        rep(i, 0, n) if (s >> i & 1) lat = i;

        int sub = s ^ (1 << lat);
        if (dp[sub] == 0) {
            dp[s] = 0;
            continue;
        }

        bool ng = false;
        for (int to : g[lat])
            if (s >> to & 1) ng = true;

        if (ng)
            dp[s] = 0;
        else
            dp[s] = 1;
    }

    dps.push_back(dp);

    while (dps.back()[(1 << n) - 1] == 0) {
        dps.push_back(bitwise_or_convolution<S, op, inv, zero>(
            dps[1], dps[dps.size() - 1]));
        rep(i, 0, n) if (dps[dps.size() - 1][i] != 0) dps[dps.size() - 1][i] =
            1;
    }

    int K = dps.size() - 1;
    vec<int> res(n, -1);

    auto dfs = [&](auto f, int S,
                   int l) -> void {  // 集合 Sを、 [l, K)を使って彩色。
        if (S == 0) return;
        int sub = S;
        do {
            if (dps[1][sub] && dps[K - l - 1][S ^ sub]) {
                rep(i, 0, n) if (sub >> i & 1) res[i] = l;

                f(f, S ^ sub, l + 1);
                break;
            }

            sub = (sub - 1) & S;
        } while (sub != S);
    };

    dfs(dfs, (1 << n) - 1, 0);

    return {K, res};
}
#line 1 "Algorithm/subset_transform.hpp"
template<class S, S (*op)(S, S)> vector<S> subset_zeta_transform (vector<S> f, int n) {
    rep(i, 0, n) {
        rep(s, 0, 1LL << n) {
            if((s & (1 << i)) != 0) { // if i in s
                f[s] = op(f[s], f[s ^ (1 << i)]);
            }
        }
    }
    return f;
}

template<class S, S (*op)(S, S), S (*inv)(S)> vector<S> subset_mobius_transform (vector<S> f, int n) {
    rrep(i, 0, n) {
        rep(s, 0, 1LL << n) {
            if((s & (1 << i)) != 0) { // if i in s
                f[s] = op(f[s], inv(f[s ^ (1 << i)]));
            }
        }
    }
    return f;
}
#line 2 "Convolution/bitwise_or_convolution.hpp"

// i or j = k <-> i or j が k の subset (⇔ i, jがともにkのsubset)
template<class S, S (*op)(S, S), S (*inv)(S),  S(*zero)()> vec<S> bitwise_or_convolution(vec<S> A, vec<S> B) {
    ll lg = 1;
    while(A.size() > (1LL << lg)) lg++;
    while(B.size() > (1LL << lg)) lg++;
    A.resize(1LL << lg, zero());
    B.resize(1LL << lg, zero());

    vec<S> FA = subset_zeta_transform<S, op>(A, lg);
    vec<S> FB = subset_zeta_transform<S, op>(B, lg);
    rep(i, 0, 1 << lg) FA[i] *= FB[i];
    vec<S> f = subset_mobius_transform<S, op, inv>(FA, lg);
    return f;
}
//以降の項について、0である。
#line 2 "Graph/Chromatic_Number_with_restoration.hpp"

using S = ll;
S op(S a, S b) {
    return a + b;
}
S inv(S x) {
    return -x;
}
S zero() {
    return 0;
}

pair<int, vec<int>> chromatic_number(const vec<vec<int>> &g) {
    if (g.empty()) return {0, {}};
    int n = g.size();

    vec<vec<ll>> dps(1, vec<ll>(1 << n, 0));
    dps[0][0] = 1;

    vec<ll> dp(1 << n, -1);  // dp[i][S] := S は i 色彩色可能か?
    dp[0] = 1;
    rep(i, 0, n) dp[1 << i] = 1;

    rep(s, 0, 1 << n) if (dp[s] == -1) {
        int lat = -1;
        rep(i, 0, n) if (s >> i & 1) lat = i;

        int sub = s ^ (1 << lat);
        if (dp[sub] == 0) {
            dp[s] = 0;
            continue;
        }

        bool ng = false;
        for (int to : g[lat])
            if (s >> to & 1) ng = true;

        if (ng)
            dp[s] = 0;
        else
            dp[s] = 1;
    }

    dps.push_back(dp);

    while (dps.back()[(1 << n) - 1] == 0) {
        dps.push_back(bitwise_or_convolution<S, op, inv, zero>(
            dps[1], dps[dps.size() - 1]));
        rep(i, 0, n) if (dps[dps.size() - 1][i] != 0) dps[dps.size() - 1][i] =
            1;
    }

    int K = dps.size() - 1;
    vec<int> res(n, -1);

    auto dfs = [&](auto f, int S,
                   int l) -> void {  // 集合 Sを、 [l, K)を使って彩色。
        if (S == 0) return;
        int sub = S;
        do {
            if (dps[1][sub] && dps[K - l - 1][S ^ sub]) {
                rep(i, 0, n) if (sub >> i & 1) res[i] = l;

                f(f, S ^ sub, l + 1);
                break;
            }

            sub = (sub - 1) & S;
        } while (sub != S);
    };

    dfs(dfs, (1 << n) - 1, 0);

    return {K, res};
}
Back to top page