Luzhiled's Library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub ei1333/library

:heavy_check_mark: test/verify/aoj-2405.test.cpp

Depends on

Code

#define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2405"

#include "../../template/template.hpp"
#include "../../math/combinatorics/mod-int.hpp"

#include "../../graph/others/tree-decomposition-width-2.hpp"

const int MOD = 1000003;
using Mint = ModInt< MOD >;

int main() {
  int N, M;
  while(cin >> N >> M, N) {
    TreeDecompositionWidth2 td(N);
    set< pair< int, int > > edges;
    for(int i = 0; i < N; i++) {
      td.add_edge(i, (i + 1) % N);
      edges.emplace(minmax(i, (i + 1) % N));
    }
    for(int i = 0; i < M; i++) {
      int a, b;
      cin >> a >> b;
      --a, --b;
      td.add_edge(a, b);
      edges.emplace(minmax(a, b));
    }

    if(N % 2) {
      cout << 0 << endl;
      continue;
    }

    auto t = td.build();
    to_nice(t);
    vector< vector< Mint > > dps(t.size());
    vector< int > buff(N), buff2(N, -1);
    MFP([&](auto rec, int idx) -> void {

      auto &ch = t[idx].child;
      auto &bag = t[idx].bag;

      for(auto &to : ch) rec(to);
      vector< Mint > dp(1 << bag.size());

      if(ch.empty()) { // leaf
        dp[0] = 1;
      } else if(ch.size() == 2) { // join
        for(int i = 0; i < dp.size(); i++) {
          for(int j = 0; j < dp.size(); j++) {
            if((i & j) == 0) dp[i | j] += dps[ch[0]][i] * dps[ch[1]][j];
          }
        }
      } else {
        auto &ch_bag = t[ch[0]].bag;
        auto &ch_dp = dps[ch[0]];

        for(int i = 0; i < bag.size(); i++) {
          buff[bag[i]] = 1 << i;
          buff2[bag[i]] = idx;
        }

        if(ch_bag.size() + 1 == bag.size()) { // introduce
          for(int i = 0; i < ch_dp.size(); i++) {
            int bit = 0;
            for(int j = 0; j < ch_bag.size(); j++) {
              if((i >> j) & 1) bit |= buff[ch_bag[j]];
            }
            dp[bit] = ch_dp[i];
          }
        } else { // forget
          int v = -1;
          for(int i = 0; i < ch_bag.size(); i++) {
            if(buff2[ch_bag[i]] != idx) v = ch_bag[i];
          }
          vector< int > ok_match(bag.size());
          for(int i = 0; i < bag.size(); i++) {
            ok_match[i] = edges.count(minmax(bag[i], v));
          }

          for(int i = 0; i < ch_dp.size(); i++) {
            int bit = 0;
            bool v_use = false;
            for(int j = 0; j < ch_bag.size(); j++) {
              if((i >> j) & 1) {
                if(v != ch_bag[j]) bit |= buff[ch_bag[j]];
              } else {
                if(v == ch_bag[j]) v_use = true;
              }
            }
            if(v_use) {
              for(int j = 0; j < bag.size(); j++) {
                if((bit >> j) & 1) continue;
                if(ok_match[j]) dp[bit | (1 << j)] += ch_dp[i];
              }
            } else {
              dp[bit] += ch_dp[i];
            }
          }
        }
      }
      dps[idx].swap(dp);
    })(0);
    auto &dp = dps[0];
    cout << dp.back() << endl;
  }
}
#line 1 "test/verify/aoj-2405.test.cpp"
#define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2405"

#line 1 "template/template.hpp"
#include<bits/stdc++.h>

using namespace std;

using int64 = long long;
const int mod = 1e9 + 7;

const int64 infll = (1LL << 62) - 1;
const int inf = (1 << 30) - 1;

struct IoSetup {
  IoSetup() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
    cerr << fixed << setprecision(10);
  }
} iosetup;

template< typename T1, typename T2 >
ostream &operator<<(ostream &os, const pair< T1, T2 >& p) {
  os << p.first << " " << p.second;
  return os;
}

template< typename T1, typename T2 >
istream &operator>>(istream &is, pair< T1, T2 > &p) {
  is >> p.first >> p.second;
  return is;
}

template< typename T >
ostream &operator<<(ostream &os, const vector< T > &v) {
  for(int i = 0; i < (int) v.size(); i++) {
    os << v[i] << (i + 1 != v.size() ? " " : "");
  }
  return os;
}

template< typename T >
istream &operator>>(istream &is, vector< T > &v) {
  for(T &in : v) is >> in;
  return is;
}

template< typename T1, typename T2 >
inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }

template< typename T1, typename T2 >
inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }

template< typename T = int64 >
vector< T > make_v(size_t a) {
  return vector< T >(a);
}

template< typename T, typename... Ts >
auto make_v(size_t a, Ts... ts) {
  return vector< decltype(make_v< T >(ts...)) >(a, make_v< T >(ts...));
}

template< typename T, typename V >
typename enable_if< is_class< T >::value == 0 >::type fill_v(T &t, const V &v) {
  t = v;
}

template< typename T, typename V >
typename enable_if< is_class< T >::value != 0 >::type fill_v(T &t, const V &v) {
  for(auto &e : t) fill_v(e, v);
}

template< typename F >
struct FixPoint : F {
  explicit FixPoint(F &&f) : F(forward< F >(f)) {}

  template< typename... Args >
  decltype(auto) operator()(Args &&... args) const {
    return F::operator()(*this, forward< Args >(args)...);
  }
};
 
template< typename F >
inline decltype(auto) MFP(F &&f) {
  return FixPoint< F >{forward< F >(f)};
}
#line 1 "math/combinatorics/mod-int.hpp"
template< int mod >
struct ModInt {
  int x;

  ModInt() : x(0) {}

  ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}

  ModInt &operator+=(const ModInt &p) {
    if((x += p.x) >= mod) x -= mod;
    return *this;
  }

  ModInt &operator-=(const ModInt &p) {
    if((x += mod - p.x) >= mod) x -= mod;
    return *this;
  }

  ModInt &operator*=(const ModInt &p) {
    x = (int) (1LL * x * p.x % mod);
    return *this;
  }

  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }

  ModInt operator-() const { return ModInt(-x); }

  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }

  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }

  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }

  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }

  bool operator==(const ModInt &p) const { return x == p.x; }

  bool operator!=(const ModInt &p) const { return x != p.x; }

  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while(b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }

  ModInt pow(int64_t n) const {
    ModInt ret(1), mul(x);
    while(n > 0) {
      if(n & 1) ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }

  friend ostream &operator<<(ostream &os, const ModInt &p) {
    return os << p.x;
  }

  friend istream &operator>>(istream &is, ModInt &a) {
    int64_t t;
    is >> t;
    a = ModInt< mod >(t);
    return (is);
  }

  static int get_mod() { return mod; }
};

using modint = ModInt< mod >;
#line 5 "test/verify/aoj-2405.test.cpp"

#line 2 "graph/others/tree-decomposition-width-2.hpp"

/**
 * @brief Tree Decomposition Width 2(木幅2の木分解)
 * @see https://ei1333.hateblo.jp/entry/2020/02/12/150319
 * @docs docs/tree-decomposition-width-2.md
 */
struct DecompNode {
  vector< int > bag, child;

  DecompNode() = default;
};

struct TreeDecompositionWidth2 {

  vector< vector< int > > g;

  explicit TreeDecompositionWidth2(int V) : g(V) {}

  void add_edge(int a, int b) {
    g[a].emplace_back(b);
    g[b].emplace_back(a);
  }

  vector< DecompNode > build() {
    const int N = (int) g.size();

    vector< int > used(N, -1), deg(N);
    queue< int > que;
    for(int i = 0; i < N; i++) {
      deg[i] = (int) g[i].size();
      if(deg[i] <= 2) que.emplace(i);
    }

    vector< set< int > > exists(N);
    for(int i = 0; i < N; i++) {
      for(auto &j : g[i]) {
        if(i < j) exists[i].emplace(j);
      }
    }

    vector< DecompNode > ret;
    ret.emplace_back();
    while(!que.empty()) {
      int idx = que.front();
      que.pop();
      if(deg[idx] > 2 || used[idx] != -1) continue;

      DecompNode r;
      r.bag.emplace_back(idx);
      int u = -1, v = -1;
      for(auto &to : g[idx]) {
        if(used[to] == -1) {
          (u == -1 ? u : v) = to;
          r.bag.emplace_back(to);
        } else if(used[to] >= 0) {
          r.child.emplace_back(used[to]);
          used[to] = -2;
        }
      }

      if(u == -1) {

      } else if(v == -1) {
        --deg[u];
      } else {
        if(u > v) swap(u, v);
        if(!exists[u].count(v)) {
          g[u].emplace_back(v);
          g[v].emplace_back(u);
          exists[u].emplace(v);
        } else {
          --deg[u];
          --deg[v];
        }
      }

      for(auto &to : g[idx]) {
        if(deg[to] <= 2) que.emplace(to);
      }

      used[idx] = (int) ret.size();
      deg[idx] = 0;
      ret.emplace_back(r);
    }
    for(int i = 0; i < N; i++) {
      if(deg[i] > 0) return {};
    }
    ret.front() = ret.back();
    ret.pop_back();
    return ret;
  }
};


void to_nice(vector< DecompNode > &g, int root = 0) {

  for(auto &p : g) {
    sort(p.bag.begin(), p.bag.end());
  }

  stack< int > st;
  st.emplace(root);

  while(!st.empty()) {
    int idx = st.top();
    st.pop();

    // join
    while(g[idx].child.size() > 2) {
      DecompNode r;
      r.child.resize(2);
      r.child[0] = g[idx].child.back();
      g[idx].child.pop_back();
      r.child[1] = g[idx].child.back();
      g[idx].child.pop_back();
      r.bag = g[idx].bag;
      g[idx].child.emplace_back((int) g.size());
      g.emplace_back(r);
    }

    if(g[idx].child.size() == 2) {
      for(auto &ch : g[idx].child) {
        if(g[ch].bag != g[idx].bag) {
          DecompNode r;
          r.child = {ch};
          r.bag = g[idx].bag;
          ch = (int) g.size();
          g.emplace_back(r);
        }
      }
    }

    // introduce / forget
    if(g[idx].child.size() == 1) {
      int &ch = g[idx].child[0];

      vector< int > latte, malta;
      set_difference(g[idx].bag.begin(), g[idx].bag.end(),
                     g[ch].bag.begin(), g[ch].bag.end(),
                     back_inserter(latte));
      set_difference(g[ch].bag.begin(), g[ch].bag.end(),
                     g[idx].bag.begin(), g[idx].bag.end(),
                     back_inserter(malta));
      if(latte.size() + malta.size() > 1) {
        DecompNode r;
        r.child = {ch};
        r.bag = g[idx].bag;
        if(!latte.empty()) {
          r.bag.erase(find(r.bag.begin(), r.bag.end(), latte.back()));
        } else {
          r.bag.emplace_back(malta.back());
        }
        ch = (int) g.size();
        g.emplace_back(r);
      }
    }

    // leaf
    if(g[idx].child.empty()) {
      if(g[idx].bag.size() > 1) {
        DecompNode r;
        r.bag = g[idx].bag;
        r.bag.pop_back();
        g[idx].child.emplace_back((int) g.size());
        g.emplace_back(r);
      }
    }

    for(auto &ch : g[idx].child) {
      st.emplace(ch);
    }
  }
}
#line 7 "test/verify/aoj-2405.test.cpp"

const int MOD = 1000003;
using Mint = ModInt< MOD >;

int main() {
  int N, M;
  while(cin >> N >> M, N) {
    TreeDecompositionWidth2 td(N);
    set< pair< int, int > > edges;
    for(int i = 0; i < N; i++) {
      td.add_edge(i, (i + 1) % N);
      edges.emplace(minmax(i, (i + 1) % N));
    }
    for(int i = 0; i < M; i++) {
      int a, b;
      cin >> a >> b;
      --a, --b;
      td.add_edge(a, b);
      edges.emplace(minmax(a, b));
    }

    if(N % 2) {
      cout << 0 << endl;
      continue;
    }

    auto t = td.build();
    to_nice(t);
    vector< vector< Mint > > dps(t.size());
    vector< int > buff(N), buff2(N, -1);
    MFP([&](auto rec, int idx) -> void {

      auto &ch = t[idx].child;
      auto &bag = t[idx].bag;

      for(auto &to : ch) rec(to);
      vector< Mint > dp(1 << bag.size());

      if(ch.empty()) { // leaf
        dp[0] = 1;
      } else if(ch.size() == 2) { // join
        for(int i = 0; i < dp.size(); i++) {
          for(int j = 0; j < dp.size(); j++) {
            if((i & j) == 0) dp[i | j] += dps[ch[0]][i] * dps[ch[1]][j];
          }
        }
      } else {
        auto &ch_bag = t[ch[0]].bag;
        auto &ch_dp = dps[ch[0]];

        for(int i = 0; i < bag.size(); i++) {
          buff[bag[i]] = 1 << i;
          buff2[bag[i]] = idx;
        }

        if(ch_bag.size() + 1 == bag.size()) { // introduce
          for(int i = 0; i < ch_dp.size(); i++) {
            int bit = 0;
            for(int j = 0; j < ch_bag.size(); j++) {
              if((i >> j) & 1) bit |= buff[ch_bag[j]];
            }
            dp[bit] = ch_dp[i];
          }
        } else { // forget
          int v = -1;
          for(int i = 0; i < ch_bag.size(); i++) {
            if(buff2[ch_bag[i]] != idx) v = ch_bag[i];
          }
          vector< int > ok_match(bag.size());
          for(int i = 0; i < bag.size(); i++) {
            ok_match[i] = edges.count(minmax(bag[i], v));
          }

          for(int i = 0; i < ch_dp.size(); i++) {
            int bit = 0;
            bool v_use = false;
            for(int j = 0; j < ch_bag.size(); j++) {
              if((i >> j) & 1) {
                if(v != ch_bag[j]) bit |= buff[ch_bag[j]];
              } else {
                if(v == ch_bag[j]) v_use = true;
              }
            }
            if(v_use) {
              for(int j = 0; j < bag.size(); j++) {
                if((bit >> j) & 1) continue;
                if(ok_match[j]) dp[bit | (1 << j)] += ch_dp[i];
              }
            } else {
              dp[bit] += ch_dp[i];
            }
          }
        }
      }
      dps[idx].swap(dp);
    })(0);
    auto &dp = dps[0];
    cout << dp.back() << endl;
  }
}
Back to top page