C++ Library for Competitive Programming
/*
* @title グラフ/彩色数
*
* verification-helper: PROBLEM https://judge.yosupo.jp/problem/chromatic_number
*/
#include <iostream>
#include <vector>
#include "emthrm/graph/chromatic_number.hpp"
#include "emthrm/graph/edge.hpp"
int main() {
int n, m;
std::cin >> n >> m;
std::vector<std::vector<emthrm::Edge<bool>>> graph(n);
while (m--) {
int u, v;
std::cin >> u >> v;
graph[u].emplace_back(u, v);
graph[v].emplace_back(v, u);
}
std::cout << emthrm::chromatic_number(graph) << '\n';
return 0;
}
#line 1 "test/graph/chromatic_number.test.cpp"
/*
* @title グラフ/彩色数
*
* verification-helper: PROBLEM https://judge.yosupo.jp/problem/chromatic_number
*/
#include <iostream>
#include <vector>
#line 1 "include/emthrm/graph/chromatic_number.hpp"
#include <bit>
#include <numeric>
#include <ranges>
#line 8 "include/emthrm/graph/chromatic_number.hpp"
#line 1 "include/emthrm/graph/edge.hpp"
/**
* @title 辺
*/
#ifndef EMTHRM_GRAPH_EDGE_HPP_
#define EMTHRM_GRAPH_EDGE_HPP_
#include <compare>
namespace emthrm {
template <typename CostType>
struct Edge {
CostType cost;
int src, dst;
explicit Edge(const int src, const int dst, const CostType cost = 0)
: cost(cost), src(src), dst(dst) {}
auto operator<=>(const Edge& x) const = default;
};
} // namespace emthrm
#endif // EMTHRM_GRAPH_EDGE_HPP_
#line 10 "include/emthrm/graph/chromatic_number.hpp"
namespace emthrm {
template <typename CostType>
int chromatic_number(const std::vector<std::vector<Edge<CostType>>>& graph) {
const int n = graph.size();
std::vector<int> adj(n, 0);
for (int i = 0; i < n; ++i) {
for (const int e : graph[i] | std::views::transform(&Edge<CostType>::dst)) {
adj[i] |= 1 << e;
}
}
std::vector<int> indep(1 << n);
indep[0] = 1;
for (unsigned int i = 1; i < (1 << n); ++i) {
const int v = std::countr_zero(i);
indep[i] = indep[i ^ (1 << v)] + indep[(i ^ (1 << v)) & ~adj[v]];
}
int res = n;
for (const int mod : std::vector<int>{1000000007, 1000000011}) {
std::vector<long long> f(1 << n);
for (unsigned int i = 0; i < (1 << n); ++i) {
f[i] = ((n - std::popcount(i)) & 1 ? mod - 1 : 1);
}
for (int c = 1; c < res; ++c) {
for (int i = 0; i < (1 << n); ++i) {
f[i] = (f[i] * indep[i]) % mod;
}
if (std::reduce(f.begin(), f.end(), 0LL) % mod > 0) {
res = c;
break;
}
}
}
return res;
}
} // namespace emthrm
#line 1 "include/emthrm/graph/edge.hpp"
/**
* @title 辺
*/
#ifndef EMTHRM_GRAPH_EDGE_HPP_
#define EMTHRM_GRAPH_EDGE_HPP_
#include <compare>
namespace emthrm {
template <typename CostType>
struct Edge {
CostType cost;
int src, dst;
explicit Edge(const int src, const int dst, const CostType cost = 0)
: cost(cost), src(src), dst(dst) {}
auto operator<=>(const Edge& x) const = default;
};
} // namespace emthrm
#endif // EMTHRM_GRAPH_EDGE_HPP_
#line 12 "test/graph/chromatic_number.test.cpp"
int main() {
int n, m;
std::cin >> n >> m;
std::vector<std::vector<emthrm::Edge<bool>>> graph(n);
while (m--) {
int u, v;
std::cin >> u >> v;
graph[u].emplace_back(u, v);
graph[v].emplace_back(v, u);
}
std::cout << emthrm::chromatic_number(graph) << '\n';
return 0;
}