結果

問題 No.2507 Yet Another Subgraph Counting
ユーザー 👑 hos.lyrichos.lyric
提出日時 2023-10-27 16:34:46
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 35 ms / 2,000 ms
コード長 8,724 bytes
コンパイル時間 1,298 ms
コンパイル使用メモリ 120,396 KB
実行使用メモリ 7,808 KB
最終ジャッジ日時 2024-09-25 13:04:31
合計ジャッジ時間 3,095 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,816 KB
testcase_01 AC 3 ms
6,944 KB
testcase_02 AC 3 ms
6,944 KB
testcase_03 AC 3 ms
6,940 KB
testcase_04 AC 3 ms
6,940 KB
testcase_05 AC 5 ms
6,940 KB
testcase_06 AC 3 ms
6,940 KB
testcase_07 AC 3 ms
6,940 KB
testcase_08 AC 3 ms
6,940 KB
testcase_09 AC 3 ms
6,940 KB
testcase_10 AC 3 ms
6,940 KB
testcase_11 AC 5 ms
6,944 KB
testcase_12 AC 3 ms
6,940 KB
testcase_13 AC 3 ms
6,944 KB
testcase_14 AC 5 ms
6,944 KB
testcase_15 AC 3 ms
6,948 KB
testcase_16 AC 3 ms
6,940 KB
testcase_17 AC 5 ms
6,940 KB
testcase_18 AC 4 ms
6,940 KB
testcase_19 AC 4 ms
6,940 KB
testcase_20 AC 3 ms
6,940 KB
testcase_21 AC 3 ms
6,940 KB
testcase_22 AC 3 ms
6,940 KB
testcase_23 AC 5 ms
6,940 KB
testcase_24 AC 4 ms
6,944 KB
testcase_25 AC 8 ms
6,960 KB
testcase_26 AC 15 ms
6,940 KB
testcase_27 AC 5 ms
6,944 KB
testcase_28 AC 8 ms
6,940 KB
testcase_29 AC 3 ms
6,940 KB
testcase_30 AC 15 ms
6,940 KB
testcase_31 AC 33 ms
7,308 KB
testcase_32 AC 33 ms
6,984 KB
testcase_33 AC 5 ms
7,808 KB
testcase_34 AC 3 ms
6,940 KB
testcase_35 AC 5 ms
6,940 KB
testcase_36 AC 3 ms
6,944 KB
testcase_37 AC 34 ms
7,436 KB
testcase_38 AC 33 ms
7,304 KB
testcase_39 AC 34 ms
6,944 KB
testcase_40 AC 35 ms
7,564 KB
testcase_41 AC 33 ms
7,180 KB
testcase_42 AC 33 ms
7,312 KB
testcase_43 AC 15 ms
6,984 KB
testcase_44 AC 33 ms
6,944 KB
testcase_45 AC 5 ms
6,940 KB
testcase_46 AC 5 ms
6,940 KB
testcase_47 AC 34 ms
6,944 KB
testcase_48 AC 3 ms
6,940 KB
testcase_49 AC 33 ms
6,944 KB
testcase_50 AC 4 ms
6,944 KB
testcase_51 AC 16 ms
6,984 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")


// as * bs
// ZT: T[2^n][n+1]
template <class T, class ZT>
vector<T> setMul(int n, const vector<T> &as, const vector<T> &bs, ZT zas, ZT zbs) {
  assert(static_cast<int>(as.size()) == 1 << n);
  assert(static_cast<int>(bs.size()) == 1 << n);
  // ranked
  for (int h = 0; h < 1 << n; ++h) {
    memset(zas[h], 0, (n + 1) * sizeof(T));
    zas[h][__builtin_popcount(h)] = as[h];
  }
  for (int h = 0; h < 1 << n; ++h) {
    memset(zbs[h], 0, (n + 1) * sizeof(T));
    zbs[h][__builtin_popcount(h)] = bs[h];
  }
  // zeta
  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) zas[h + w][k] += zas[h][k];
    }
  }
  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];
    }
  }
  // product
  for (int h = 0; h < 1 << n; ++h) {
    for (int k = n; k >= 0; --k) {
      T t = 0;
      for (int l = 0; l <= k; ++l) t += zas[h][l] * zbs[h][k - l];
      zas[h][k] = t;
    }
  }
  // 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) zas[h + w][k] -= zas[h][k];
    }
  }
  // unrank
  vector<T> cs(1 << n);
  for (int h = 0; h < 1 << n; ++h) cs[h] = zas[h][__builtin_popcount(h)];
  return cs;
}

// 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;
}

// \sum[0<=i<=n] fs[i] as^i/i!
//   assume as[0] == 0
//   f(a0 + a1 X) + f(a0) + f'(a0) a1 X
// ZT1: T[2^(n-1)][n]
// ZT: T[2^(n+1)][n+1]
template <class T, class ZT1, class ZT>
vector<T> setCom(int n, const vector<T> &fs, const vector<T> &as, ZT1 zas, ZT zbs) {
  assert(static_cast<int>(fs.size()) == n + 1);
  assert(static_cast<int>(as.size()) == 1 << n);
  assert(as[0] == 0);
  // zbs[2^(n-i), 2^(n-i+1)): composite f^(i)
  for (int i = 0; i <= n; ++i) zbs[1<<(n-i)][0] = fs[i];
  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 i = 0; i < n - m; ++i) {
      for (int h = 0; h < 1 << m; ++h) {
        // zeta
        zbs[(1<<(n-i)) + h][m + 1] = 0;
        memcpy(zbs[(1<<(n-i)) + (1 << m) + h], zbs[(1<<(n-i)) + h], ((m + 1) + 1) * sizeof(T));
        // product
        for (int k = 0; k <= m; ++k) for (int l = 0; l <= m - k; ++l) {
          zbs[(1<<(n-i)) + (1 << m) + h][k + l + 1] += zbs[(1<<(n-(i+1))) + 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[(1<<n) + h + w][k] -= zbs[(1<<n) + h][k];
    }
  }
  // unrank
  vector<T> bs(1 << n);
  for (int h = 0; h < 1 << n; ++h) bs[h] = zbs[(1<<n) + h][__builtin_popcount(h)];
  return bs;
}

// \sum[i] fs[i] as^i
//   not necessarily as[0] == 0
// ZT1: T[2^(n-1)][n]
// ZT: T[2^(n+1)][n+1]
template <class T, class ZT1, class ZT>
vector<T> setComPoly(int n, vector<T> fs, vector<T> as, ZT1 zas, ZT zbs) {
  assert(static_cast<int>(as.size()) == 1 << n);
  const int fsLen = fs.size();
  if (fsLen == 0) return vector<T>(1 << n, 0);
  vector<T> gs(n + 1);
  for (int i = 0; i <= n; ++i) {
    T t = 0;
    for (int j = fsLen; --j >= 0; ) (t *= as[0]) += fs[j];
    gs[i] = t;
    for (int j = 1; j < fsLen; ++j) fs[j - 1] = j * fs[j];
    fs[fsLen - 1] = 0;
  }
  as[0] = 0;
  return setCom(n, gs, as, zas, zbs);
}

////////////////////////////////////////////////////////////////////////////////

using U = unsigned long long;

constexpr int MAX_N = 13;
U zas[1 << (MAX_N + 1)][MAX_N + 1];
U zbs[1 << (MAX_N + 1)][MAX_N + 1];


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

U dp[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];
      if (A[i] > B[i]) {
        swap(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];
    }
    
    // cycle
    vector<U> cs(1 << N, 0);
    memset(dp, 0, sizeof(dp));
    for (int r = 0; r < N; ++r) {
      dp[1 << r][r] = 1;
      for (int p = 1 << r; p < 1 << (r + 1); ++p) {
        for (int u = 0; u <= r; ++u) if (p & 1 << u) {
          for (int v = 0; v < r; ++v) if (!(p & 1 << v)) {
            if (adj[u] >> v & 1) {
              dp[p | 1 << v][v] += dp[p][u];
            }
          }
          if (adj[u] >> r & 1) {
            cs[p] += dp[p][u];
          }
        }
      }
    }
    for (int i = 0; i < M; ++i) cs[1 << A[i] | 1 << B[i]] -= 1;
    for (int p = 0; p < 1 << N; ++p) cs[p] /= 2;
// cerr<<"cs = "<<cs<<endl;
    for (int u = 0; u < N; ++u) cs[1 << u] += 1;
    
    fill(adj.begin(), adj.end(), 0);
    for (int i = 0; i < M; ++i) {
      adj[B[i]] |= 1 << A[i];
    }
    for (int r = 1; r < N; ++r) {
      /*
        cs[p]:
          before: connected graph on p, allow bridge (u, v) s.t. u < v < r
          after : connected graph on p, allow bridge (u, v) s.t. u < v <= r
      */
      vector<U> ds(1 << (N - 1)), es(1 << (N - 1));
      for (int p = 0; p < 1 << r; ++p) for (int q = 0; q < 1 << (N-1-r); ++q) {
        ds[p | q << r] = cs[p | 1 << r | q << (r + 1)];
        es[p | q << r] = __builtin_popcount(p & adj[r]) * cs[p | q << (r + 1)];
      }
      es = setExp(N - 1, es, zas, zbs);
      ds = setMul(N - 1, ds, es, zas, zbs);
      for (int p = 0; p < 1 << r; ++p) for (int q = 0; q < 1 << (N-1-r); ++q) {
        cs[p | 1 << r | q << (r + 1)] = ds[p | q << r];
      }
// cerr<<"cs = "<<cs<<endl;
    }
    
    cs = setExp(N, cs, zas, zbs);
    printf("%llu\n", cs.back());
  }
  return 0;
}
0