結果

問題 No.2507 Yet Another Subgraph Counting
ユーザー 👑 emthrmemthrm
提出日時 2023-08-27 02:49:07
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 890 ms / 2,000 ms
コード長 5,824 bytes
コンパイル時間 1,786 ms
コンパイル使用メモリ 145,524 KB
実行使用メモリ 6,640 KB
最終ジャッジ日時 2024-09-15 14:16:30
合計ジャッジ時間 7,327 ms
ジャッジサーバーID
(参考情報)
judge4 / 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 6 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 7 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 8 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 8 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 3 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 15 ms
5,376 KB
testcase_24 AC 3 ms
5,376 KB
testcase_25 AC 18 ms
5,376 KB
testcase_26 AC 121 ms
5,376 KB
testcase_27 AC 10 ms
5,376 KB
testcase_28 AC 24 ms
5,376 KB
testcase_29 AC 3 ms
5,376 KB
testcase_30 AC 144 ms
5,376 KB
testcase_31 AC 71 ms
6,388 KB
testcase_32 AC 72 ms
6,516 KB
testcase_33 AC 7 ms
5,376 KB
testcase_34 AC 3 ms
5,376 KB
testcase_35 AC 6 ms
5,376 KB
testcase_36 AC 3 ms
5,376 KB
testcase_37 AC 318 ms
6,388 KB
testcase_38 AC 869 ms
6,388 KB
testcase_39 AC 99 ms
6,384 KB
testcase_40 AC 438 ms
6,516 KB
testcase_41 AC 749 ms
6,516 KB
testcase_42 AC 890 ms
6,516 KB
testcase_43 AC 30 ms
5,376 KB
testcase_44 AC 70 ms
6,512 KB
testcase_45 AC 7 ms
5,376 KB
testcase_46 AC 7 ms
5,376 KB
testcase_47 AC 68 ms
6,640 KB
testcase_48 AC 3 ms
5,376 KB
testcase_49 AC 107 ms
6,520 KB
testcase_50 AC 4 ms
5,376 KB
testcase_51 AC 30 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <array>
#include <bit>
#include <cassert>
#include <concepts>
#include <cstdint>
#include <functional>
#include <iostream>
#include <iterator>
#include <utility>
#include <vector>

namespace emthrm {

template <int MaxN, typename T>
std::vector<T> subset_convolution(
    const std::vector<T>& f, const std::vector<T>& g) {
  using Polynomial = std::array<T, MaxN + 1>;
  assert(std::has_single_bit(f.size()) && f.size() == g.size());
  const int n = std::countr_zero(f.size());
  assert(n <= MaxN);
  const int domain_size = 1 << n;
  const auto ranked_zeta_transform =
      [n, domain_size](const std::vector<T>& f) -> std::vector<Polynomial> {
    std::vector a(domain_size, Polynomial{});
    for (int i = 0; i < domain_size; ++i) {
      a[i][std::popcount(static_cast<unsigned int>(i))] = f[i];
    }
    for (int bit = 1; bit < domain_size; bit <<= 1) {
      for (int i = 0; i < domain_size; ++i) {
        if ((i & bit) == 0) {
          for (int degree = 0; degree <= n; ++degree) {
            a[i | bit][degree] += a[i][degree];
          }
        }
      }
    }
    return a;
  };
  std::vector<Polynomial> a = ranked_zeta_transform(f);
  const std::vector<Polynomial> b = ranked_zeta_transform(g);
  for (int i = 0; i < domain_size; ++i) {
    // Hadamard product
    for (int degree_of_a = n; degree_of_a >= 0; --degree_of_a) {
      const T tmp = std::exchange(a[i][degree_of_a], T{});
      for (int degree_of_b = 0; degree_of_a + degree_of_b <= n; ++degree_of_b) {
        a[i][degree_of_a + degree_of_b] += tmp * b[i][degree_of_b];
      }
    }
  }
  for (int bit = 1; bit < domain_size; bit <<= 1) {
    for (int i = 0; i < domain_size; ++i) {
      if ((i & bit) == 0) {
        for (int degree = 0; degree <= n; ++degree) {
          a[i | bit][degree] -= a[i][degree];
        }
      }
    }
  }
  std::vector<T> c(domain_size);
  for (int i = 0; i < domain_size; ++i) {
    c[i] = a[i][std::popcount(static_cast<unsigned int>(i))];
  }
  return c;
}

// ここ
template <int MaxN, typename T>
std::vector<T> exp_of_set_power_series(const std::vector<T>& f) {
  assert(std::has_single_bit(f.size()) && f[0] == 0);
  const int n = std::countr_zero(f.size());
  assert(n <= MaxN);
  std::vector<T> exponential = f, tmp = f;
  exponential.front() = 1;
  T coef = 1;
  for (int k = 2; k <= n; ++k) {
    tmp = emthrm::subset_convolution<MaxN>(tmp, f);
    coef *= k;
    std::ranges::transform(
        tmp, exponential, exponential.begin(), std::plus<T>(),
        [coef](const T tmp_i) -> T { return tmp_i / coef; });
  }
  return exponential;
}

}  // namespace emthrm

bool KthBit(const std::unsigned_integral auto bit_field, const int k) {
  return std::cmp_equal(bit_field >> k & 1, 1);
}

// <AC> #906824 ベース
// O(3^n n^3) 時間
int main() {
  constexpr int kMaxN = 13;

  int n, m;
  std::cin >> n >> m;
  assert(2 <= n && n <= kMaxN && 1 <= m && m <= n * (n - 1) / 2);
  std::vector<std::uint32_t> graph(n);
  while (m--) {
    int u, v;
    std::cin >> u >> v;
    assert(1 <= u && u < v && v <= n);
    --u; --v;
    graph[u] |= UINT32_C(1) << v;
    graph[v] |= UINT32_C(1) << u;
  }

  std::vector num_of_paths(1 << n, std::vector(n, INT64_C(0)));
  for (int start = 0; start < n; ++start) {
    num_of_paths[1 << start][start] = 1;
  }
  for (std::uint32_t bit = 1; bit < (UINT32_C(1) << n); ++bit) {
    const int start = std::countr_zero(bit);
    for (int current_vertex = start; current_vertex < n; ++current_vertex) {
      for (int next_vertex = start + 1; next_vertex < n; ++next_vertex) {
        if (!KthBit(bit, next_vertex) &&
            KthBit(graph[current_vertex], next_vertex)) {
          num_of_paths[bit | (1 << next_vertex)][next_vertex] +=
              num_of_paths[bit][current_vertex];
        }
      }
    }
  }

  std::vector<std::int64_t> num_of_cycles(1 << n, 0);
  for (int vertex = 0; vertex < n; ++vertex) {
    num_of_cycles[1 << vertex] = 1;
  }
  for (std::uint32_t bit = 7; bit < (UINT32_C(1) << n); ++bit) {
    if (std::popcount(bit) < 3) continue;
    const int start = std::countr_zero(bit);
    for (int last = start + 1; last < n; ++last) {
      if (KthBit(bit, last) && KthBit(graph[last], start)) {
        num_of_cycles[bit] += num_of_paths[bit][last];
      }
    }
    num_of_cycles[bit] /= 2;
  }

  std::vector<std::uint64_t> dp(1 << n, 0);
  dp[1] = 1;
  for (int vertex = 1; vertex < n; ++vertex) {
    for (std::uint32_t cycle = UINT32_C(1) << vertex;
         cycle < (UINT32_C(1) << (vertex + 1)); ++cycle) {
      if (num_of_cycles[cycle] == 0) continue;

      std::vector<int> on_cycle;
      on_cycle.reserve(std::popcount(cycle));
      for (int i = 0; i <= vertex; ++i) {
        if (KthBit(cycle, i)) on_cycle.emplace_back(i);
      }

      const int size = 1 << (vertex + 1 - on_cycle.size());
      std::vector<std::uint32_t> bit_fields;
      bit_fields.reserve(size);
      const std::uint32_t ground_set =
          ((UINT32_C(1) << (vertex + 1)) - 1) ^ cycle;
      for (std::uint32_t subset = ground_set; ;
           subset = (subset - 1) & ground_set) {
        bit_fields.emplace_back(subset);
        if (subset == UINT32_C(0)) break;
      }
      assert(std::ssize(bit_fields) == size);
      std::ranges::sort(bit_fields);

      std::vector<std::uint64_t> tmp(size);
      for (int i = 0; i < size; ++i) {
        int degree = 0;
        for (const int v : on_cycle) {
          degree += std::popcount(bit_fields[i] & graph[v]);
        }
        tmp[i] = dp[bit_fields[i]] * degree;
      }
      tmp = emthrm::exp_of_set_power_series<kMaxN>(tmp);
      for (int i = 0; i < size; ++i) {
        dp[bit_fields[i] | cycle] += tmp[i] * num_of_cycles[cycle];
      }
    }
  }

  std::cout << emthrm::exp_of_set_power_series<kMaxN>(dp)[(1 << n) - 1] << '\n';
  return 0;
}
0