結果

問題 No.2507 Yet Another Subgraph Counting
ユーザー 👑 hos.lyrichos.lyric
提出日時 2023-10-13 22:03:40
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 84 ms / 2,000 ms
コード長 5,020 bytes
コンパイル時間 1,263 ms
コンパイル使用メモリ 116,524 KB
実行使用メモリ 5,676 KB
最終ジャッジ日時 2023-10-13 22:03:44
合計ジャッジ時間 3,784 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,352 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,352 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,352 KB
testcase_06 AC 2 ms
4,352 KB
testcase_07 AC 1 ms
4,356 KB
testcase_08 AC 2 ms
4,352 KB
testcase_09 AC 1 ms
4,352 KB
testcase_10 AC 1 ms
4,348 KB
testcase_11 AC 2 ms
4,352 KB
testcase_12 AC 1 ms
4,352 KB
testcase_13 AC 1 ms
4,372 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 1 ms
4,348 KB
testcase_16 AC 2 ms
4,352 KB
testcase_17 AC 2 ms
4,352 KB
testcase_18 AC 2 ms
4,352 KB
testcase_19 AC 2 ms
4,352 KB
testcase_20 AC 2 ms
4,352 KB
testcase_21 AC 2 ms
4,352 KB
testcase_22 AC 1 ms
4,352 KB
testcase_23 AC 3 ms
4,352 KB
testcase_24 AC 2 ms
4,352 KB
testcase_25 AC 4 ms
4,352 KB
testcase_26 AC 16 ms
4,616 KB
testcase_27 AC 3 ms
4,352 KB
testcase_28 AC 4 ms
4,352 KB
testcase_29 AC 1 ms
4,348 KB
testcase_30 AC 17 ms
4,544 KB
testcase_31 AC 7 ms
5,444 KB
testcase_32 AC 6 ms
5,604 KB
testcase_33 AC 2 ms
4,352 KB
testcase_34 AC 2 ms
4,348 KB
testcase_35 AC 2 ms
4,352 KB
testcase_36 AC 2 ms
4,348 KB
testcase_37 AC 33 ms
5,536 KB
testcase_38 AC 83 ms
5,600 KB
testcase_39 AC 10 ms
5,676 KB
testcase_40 AC 45 ms
5,540 KB
testcase_41 AC 73 ms
5,448 KB
testcase_42 AC 84 ms
5,528 KB
testcase_43 AC 4 ms
4,540 KB
testcase_44 AC 7 ms
5,536 KB
testcase_45 AC 3 ms
4,348 KB
testcase_46 AC 2 ms
4,352 KB
testcase_47 AC 6 ms
5,488 KB
testcase_48 AC 2 ms
4,348 KB
testcase_49 AC 10 ms
5,472 KB
testcase_50 AC 2 ms
4,352 KB
testcase_51 AC 4 ms
4,552 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")


// exp(as)
//   assume as[0] == 0
//   exp(a0 + a1 X) = exp(a0) + exp(a0) a1 X
// ZT1: T[2^(n-1)][n]
// ZT: T[2^n][n+1]
template <class T, class ZT1, class ZT>
vector<T> setExp(int n, const vector<T> &as, ZT1 zas, ZT zbs) {
  assert(static_cast<int>(as.size()) == 1 << n);
  assert(as[0] == 0);
  zbs[0][0] = 1;
  for (int m = 0; m < n; ++m) {
    // ranked a[2^m, 2^(m+1))
    for (int h = 0; h < 1 << m; ++h) {
      memset(zas[h], 0, (m + 1) * sizeof(T));
      zas[h][__builtin_popcount(h)] = as[(1 << m) + h];
    }
    // zeta
    for (int w = 1; w < 1 << m; w <<= 1) {
      for (int h0 = 0; h0 < 1 << m; h0 += w << 1) for (int h = h0; h < h0 + w; ++h) {
        for (int k = 0; k <= m; ++k) zas[h + w][k] += zas[h][k];
      }
    }
    for (int h = 0; h < 1 << m; ++h) {
      // zeta
      zbs[h][m + 1] = 0;
      memcpy(zbs[(1 << m) + h], zbs[h], ((m + 1) + 1) * sizeof(T));
      // product
      for (int k = 0; k <= m; ++k) for (int l = 0; l <= m - k; ++l) {
        zbs[(1 << m) + h][k + l + 1] += zbs[h][k] * zas[h][l];
      }
    }
  }
  // moebius
  for (int w = 1; w < 1 << n; w <<= 1) {
    for (int h0 = 0; h0 < 1 << n; h0 += w << 1) for (int h = h0; h < h0 + w; ++h) {
      for (int k = 0; k <= n; ++k) zbs[h + w][k] -= zbs[h][k];
    }
  }
  // unrank
  vector<T> bs(1 << n);
  for (int h = 0; h < 1 << n; ++h) bs[h] = zbs[h][__builtin_popcount(h)];
  return bs;
}


using U = unsigned long long;
constexpr int MAX_N = 13;

int N, M;
vector<int> A, B;

U zas[1 << MAX_N][MAX_N + 1];
U zbs[1 << MAX_N][MAX_N + 1];

U sub[1 << MAX_N][MAX_N];

int main() {
  for (; ~scanf("%d%d", &N, &M); ) {
    A.resize(M);
    B.resize(M);
    for (int i = 0; i < M; ++i) {
      scanf("%d%d", &A[i], &B[i]);
      --A[i];
      --B[i];
    }
    
    vector<int> adj(N, 0);
    for (int i = 0; i < M; ++i) {
      adj[A[i]] |= 1 << B[i];
      adj[B[i]] |= 1 << A[i];
    }
    
    vector<U> cyc(1 << N, 0);
    for (int s = 0; s < N; ++s) {
      for (int p = 0; p < 1 << s; ++p) {
        fill(sub[p], sub[p] + (s + 1), 0);
      }
      sub[0][s] = 1;
      for (int p = 0; p < 1 << s; ++p) for (int u = 0; u <= s; ++u) if (sub[p][u]) {
        for (int v = 0; v < s; ++v) if ((adj[u] >> v & 1) && !(p & 1 << v)) {
          sub[p | 1 << v][v] += sub[p][u];
        }
        if (p != 1 << u && (adj[u] >> s & 1)) {
          cyc[p | 1 << s] += sub[p][u];
        }
      }
      cyc[1 << s] = 1;
      for (int p = 1; p < 1 << s; ++p) if (cyc[p | 1 << s]) {
        assert(__builtin_popcount(p) >= 2);
        assert(cyc[p | 1 << s] % 2 == 0);
        cyc[p | 1 << s] /= 2;
      }
    }
// cerr<<"cyc = "<<cyc<<endl;
    
    vector<U> tree(1 << N, 0);
    for (int s = 0; s < N; ++s) {
      for (int p = 1 << s; p < 1 << (s + 1); ++p) if (cyc[p]) {
        vector<int> us;
        for (int u = 0; u < s; ++u) if (!(p & 1 << u)) {
          us.push_back(u);
        }
        const int usLen = us.size();
        vector<int> qs(1 << usLen, 0);
        vector<int> ways(1 << usLen, 0);
        for (int x = 0; x < usLen; ++x) {
          const int deg = __builtin_popcount(adj[us[x]] & p);
          for (int h = 0; h < 1 << x; ++h) {
            qs[h | 1 << x] = qs[h] | 1 << us[x];
            ways[h | 1 << x] = ways[h] + deg;
          }
        }
        vector<U> fs(1 << usLen, 0);
        for (int h = 0; h < 1 << usLen; ++h) {
          fs[h] = ways[h] * tree[qs[h]];
        }
        const auto gs = setExp(usLen, fs, zas, zbs);
// cerr<<s<<" "<<p<<" "<<us<<" "<<fs<<" "<<gs<<endl;
        for (int h = 0; h < 1 << usLen; ++h) {
          tree[p | qs[h]] += cyc[p] * gs[h];
        }
      }
    }
    const auto ans = setExp(N, tree, zas, zbs);
// cerr<<"tree = "<<tree<<endl;
// cerr<<"ans = "<<ans<<endl;
    printf("%llu\n", ans.back());
  }
  return 0;
}
0