This documentation is automatically generated by online-judge-tools/verification-helper
View the Project on GitHub hitonanode/cplib-cpp
#include "../enumerate_cliques.hpp" #include "../../modint.hpp" #include <iostream> #include <vector> using namespace std; #define PROBLEM "https://judge.yosupo.jp/problem/enumerate_cliques" int main() { cin.tie(nullptr), ios::sync_with_stdio(false); int N, M; cin >> N >> M; using mint = ModInt998244353; vector<mint> X(N); for (auto &x : X) cin >> x; enumerate_cliques ec(N); while (M--) { int u, v; cin >> u >> v; ec.add_bi_edge(u, v); } mint ret = mint(); auto op = [&](const std::vector<int> &clique) -> void { mint tmp = 1; for (int x : clique) tmp *= X.at(x); ret += tmp; }; ec.run(op); cout << ret << endl; }
#line 2 "graph/enumerate_cliques.hpp" /** * @file enumerate_cliques.hpp * @brief Enumerate all cliques of given undirected graph * @author hitonanode * @date 2023/03/10 */ #include <algorithm> #include <cassert> #include <numeric> #include <queue> #include <utility> #include <vector> // Complexity: O(2^sqrt(2m) * n) // Verify: https://judge.yosupo.jp/problem/enumerate_cliques (~1ms for n <= 100, m <= 100) // p.15, https://www.slideshare.net/wata_orz/ss-12131479 struct enumerate_cliques { std::vector<std::vector<int>> to; std::vector<std::pair<int, int>> edges; int n() const { return to.size(); } int m() const { return edges.size(); } enumerate_cliques(int n_) : to(n_) {} void add_bi_edge(int u, int v) { assert(0 <= u and u < n()); assert(0 <= v and v < n()); if (u != v) edges.emplace_back(std::minmax(u, v)); } // Build `to` void precalc() { std::sort(edges.begin(), edges.end()); edges.erase(std::unique(edges.begin(), edges.end()), edges.end()); for (auto &vec : to) vec.clear(); for (auto [u, v] : edges) to.at(u).push_back(v), to.at(v).push_back(u); for (auto &vec : to) std::sort(vec.begin(), vec.end()); } template <class F> void bruteforce(const std::vector<int> &cand_vs, int prefix_use, F op) const { const int k = cand_vs.size(); const int mask_all = (1 << k) - 1; std::vector<int> ok_masks(k, mask_all); for (int i = 0; i < k; ++i) { for (int j = 0; j < i; ++j) { int u = cand_vs.at(i), v = cand_vs.at(j); if (!std::binary_search(to.at(u).cbegin(), to.at(u).cend(), v)) { ok_masks.at(i) &= ~(1 << j), ok_masks.at(j) &= ~(1 << i); } } } std::vector<int> seq; if (prefix_use >= 0) seq.push_back(prefix_use); seq.reserve(seq.size() + k); auto rec = [&](auto &&self, int now, const int last_mask) -> void { op(seq); seq.push_back(-1); for (int i = now; i < k; ++i) { if ((last_mask >> i) & 1) { seq.back() = cand_vs.at(i); self(self, i + 1, last_mask & ok_masks.at(i)); } } seq.pop_back(); }; rec(rec, 0, mask_all); return; } template <class F> void run(F op) { precalc(); std::vector<int> deg(n()), is_alive(n(), 1); using P = std::pair<int, int>; std::priority_queue<P, std::vector<P>, std::greater<P>> pq; for (int i = 0; i < n(); ++i) deg.at(i) = to.at(i).size(), pq.emplace(deg.at(i), i); int rem_n = n(), rem_m = m(); std::vector<int> cand_vs; while (!pq.empty()) { auto [di, i] = pq.top(); pq.pop(); if (deg.at(i) != di) continue; cand_vs.clear(); if (di * di > rem_m * 2) { // Avoid "deg[i] = 0" case for (int i = 0; i < n(); ++i) { if (is_alive.at(i)) cand_vs.push_back(i); } bruteforce(cand_vs, -1, op); break; } for (int j : to.at(i)) { if (is_alive.at(j)) cand_vs.push_back(j); } bruteforce(cand_vs, i, op); --rem_n, deg.at(i) = 0, is_alive.at(i) = 0; for (int j : cand_vs) --rem_m, --deg.at(j), pq.emplace(deg.at(j), j); } } }; #line 3 "modint.hpp" #include <iostream> #include <set> #line 6 "modint.hpp" template <int md> struct ModInt { using lint = long long; constexpr static int mod() { return md; } static int get_primitive_root() { static int primitive_root = 0; if (!primitive_root) { primitive_root = [&]() { std::set<int> fac; int v = md - 1; for (lint i = 2; i * i <= v; i++) while (v % i == 0) fac.insert(i), v /= i; if (v > 1) fac.insert(v); for (int g = 1; g < md; g++) { bool ok = true; for (auto i : fac) if (ModInt(g).pow((md - 1) / i) == 1) { ok = false; break; } if (ok) return g; } return -1; }(); } return primitive_root; } int val_; int val() const noexcept { return val_; } constexpr ModInt() : val_(0) {} constexpr ModInt &_setval(lint v) { return val_ = (v >= md ? v - md : v), *this; } constexpr ModInt(lint v) { _setval(v % md + md); } constexpr explicit operator bool() const { return val_ != 0; } constexpr ModInt operator+(const ModInt &x) const { return ModInt()._setval((lint)val_ + x.val_); } constexpr ModInt operator-(const ModInt &x) const { return ModInt()._setval((lint)val_ - x.val_ + md); } constexpr ModInt operator*(const ModInt &x) const { return ModInt()._setval((lint)val_ * x.val_ % md); } constexpr ModInt operator/(const ModInt &x) const { return ModInt()._setval((lint)val_ * x.inv().val() % md); } constexpr ModInt operator-() const { return ModInt()._setval(md - val_); } constexpr ModInt &operator+=(const ModInt &x) { return *this = *this + x; } constexpr ModInt &operator-=(const ModInt &x) { return *this = *this - x; } constexpr ModInt &operator*=(const ModInt &x) { return *this = *this * x; } constexpr ModInt &operator/=(const ModInt &x) { return *this = *this / x; } friend constexpr ModInt operator+(lint a, const ModInt &x) { return ModInt(a) + x; } friend constexpr ModInt operator-(lint a, const ModInt &x) { return ModInt(a) - x; } friend constexpr ModInt operator*(lint a, const ModInt &x) { return ModInt(a) * x; } friend constexpr ModInt operator/(lint a, const ModInt &x) { return ModInt(a) / x; } constexpr bool operator==(const ModInt &x) const { return val_ == x.val_; } constexpr bool operator!=(const ModInt &x) const { return val_ != x.val_; } constexpr bool operator<(const ModInt &x) const { return val_ < x.val_; } // To use std::map<ModInt, T> friend std::istream &operator>>(std::istream &is, ModInt &x) { lint t; return is >> t, x = ModInt(t), is; } constexpr friend std::ostream &operator<<(std::ostream &os, const ModInt &x) { return os << x.val_; } constexpr ModInt pow(lint n) const { ModInt ans = 1, tmp = *this; while (n) { if (n & 1) ans *= tmp; tmp *= tmp, n >>= 1; } return ans; } static constexpr int cache_limit = std::min(md, 1 << 21); static std::vector<ModInt> facs, facinvs, invs; constexpr static void _precalculation(int N) { const int l0 = facs.size(); if (N > md) N = md; if (N <= l0) return; facs.resize(N), facinvs.resize(N), invs.resize(N); for (int i = l0; i < N; i++) facs[i] = facs[i - 1] * i; facinvs[N - 1] = facs.back().pow(md - 2); for (int i = N - 2; i >= l0; i--) facinvs[i] = facinvs[i + 1] * (i + 1); for (int i = N - 1; i >= l0; i--) invs[i] = facinvs[i] * facs[i - 1]; } constexpr ModInt inv() const { if (this->val_ < cache_limit) { if (facs.empty()) facs = {1}, facinvs = {1}, invs = {0}; while (this->val_ >= int(facs.size())) _precalculation(facs.size() * 2); return invs[this->val_]; } else { return this->pow(md - 2); } } constexpr ModInt fac() const { while (this->val_ >= int(facs.size())) _precalculation(facs.size() * 2); return facs[this->val_]; } constexpr ModInt facinv() const { while (this->val_ >= int(facs.size())) _precalculation(facs.size() * 2); return facinvs[this->val_]; } constexpr ModInt doublefac() const { lint k = (this->val_ + 1) / 2; return (this->val_ & 1) ? ModInt(k * 2).fac() / (ModInt(2).pow(k) * ModInt(k).fac()) : ModInt(k).fac() * ModInt(2).pow(k); } constexpr ModInt nCr(int r) const { if (r < 0 or this->val_ < r) return ModInt(0); return this->fac() * (*this - r).facinv() * ModInt(r).facinv(); } constexpr ModInt nPr(int r) const { if (r < 0 or this->val_ < r) return ModInt(0); return this->fac() * (*this - r).facinv(); } static ModInt binom(int n, int r) { static long long bruteforce_times = 0; if (r < 0 or n < r) return ModInt(0); if (n <= bruteforce_times or n < (int)facs.size()) return ModInt(n).nCr(r); r = std::min(r, n - r); ModInt ret = ModInt(r).facinv(); for (int i = 0; i < r; ++i) ret *= n - i; bruteforce_times += r; return ret; } // Multinomial coefficient, (k_1 + k_2 + ... + k_m)! / (k_1! k_2! ... k_m!) // Complexity: O(sum(ks)) template <class Vec> static ModInt multinomial(const Vec &ks) { ModInt ret{1}; int sum = 0; for (int k : ks) { assert(k >= 0); ret *= ModInt(k).facinv(), sum += k; } return ret * ModInt(sum).fac(); } // Catalan number, C_n = binom(2n, n) / (n + 1) // C_0 = 1, C_1 = 1, C_2 = 2, C_3 = 5, C_4 = 14, ... // https://oeis.org/A000108 // Complexity: O(n) static ModInt catalan(int n) { if (n < 0) return ModInt(0); return ModInt(n * 2).fac() * ModInt(n + 1).facinv() * ModInt(n).facinv(); } ModInt sqrt() const { if (val_ == 0) return 0; if (md == 2) return val_; if (pow((md - 1) / 2) != 1) return 0; ModInt b = 1; while (b.pow((md - 1) / 2) == 1) b += 1; int e = 0, m = md - 1; while (m % 2 == 0) m >>= 1, e++; ModInt x = pow((m - 1) / 2), y = (*this) * x * x; x *= (*this); ModInt z = b.pow(m); while (y != 1) { int j = 0; ModInt t = y; while (t != 1) j++, t *= t; z = z.pow(1LL << (e - j - 1)); x *= z, z *= z, y *= z; e = j; } return ModInt(std::min(x.val_, md - x.val_)); } }; template <int md> std::vector<ModInt<md>> ModInt<md>::facs = {1}; template <int md> std::vector<ModInt<md>> ModInt<md>::facinvs = {1}; template <int md> std::vector<ModInt<md>> ModInt<md>::invs = {0}; using ModInt998244353 = ModInt<998244353>; // using mint = ModInt<998244353>; // using mint = ModInt<1000000007>; #line 5 "graph/test/enumerate_cliques.test.cpp" using namespace std; #define PROBLEM "https://judge.yosupo.jp/problem/enumerate_cliques" int main() { cin.tie(nullptr), ios::sync_with_stdio(false); int N, M; cin >> N >> M; using mint = ModInt998244353; vector<mint> X(N); for (auto &x : X) cin >> x; enumerate_cliques ec(N); while (M--) { int u, v; cin >> u >> v; ec.add_bi_edge(u, v); } mint ret = mint(); auto op = [&](const std::vector<int> &clique) -> void { mint tmp = 1; for (int x : clique) tmp *= X.at(x); ret += tmp; }; ec.run(op); cout << ret << endl; }