Tifa's CP Library

:warning: dlx (src/code/util/dlx.hpp)

Depends on

Required by

Code

#ifndef TIFALIBS_UTIL_DLX
#define TIFALIBS_UTIL_DLX

#include "util.hpp"

namespace tifa_libs::util {

class DLX {
  struct node {
    u32 l, r, u, d, row, col;
    constexpr node(u32 l = 0, u32 r = 0, u32 u = 0, u32 d = 0, u32 row = 0, u32 col = 0) : l(l), r(r), u(u), d(d), row(row), col(col) {}
  };
  vec<node> data;
  vecu cnt_col;
  const bool mans;

#define l_(x) data[x].l
#define r_(x) data[x].r
#define u_(x) data[x].u
#define d_(x) data[x].d
#define row_(x) data[x].row
#define col_(x) data[x].col
#define dlxfor_(i, l, dir) for (u32 ied__ = (l), i = dir##_(ied__); i != ied__; i = dir##_(i))

  constexpr void remove_(u32 col) {
    r_(l_(col)) = r_(col);
    l_(r_(col)) = l_(col);
    dlxfor_ (i, col, d)
      dlxfor_ (j, i, r) {
        u_(d_(j)) = u_(j);
        d_(u_(j)) = d_(j);
        --cnt_col[col_(j)];
      }
  }
  constexpr void resume_(u32 col) {
    r_(l_(col)) = l_(r_(col)) = col;
    dlxfor_ (i, col, u)
      dlxfor_ (j, i, r) {
        u_(d_(j)) = d_(u_(j)) = j;
        ++cnt_col[col_(j)];
      }
  }
  template <class F>
  constexpr bool dance_(vecu &ans, F &&cb) {
    u32 now = r_(0);
    if (now == 0) return cb(ans), true;
    dlxfor_ (i, 0, r)
      if (cnt_col[i] < cnt_col[now]) now = i;
    remove_(now);
    bool ret = false;
    dlxfor_ (i, now, d) {
      ans.push_back(row_(i));
      dlxfor_ (j, i, r) remove_(col_(j));
      ret |= dance_(ans, std::forward<F>(cb));
      dlxfor_ (j, i, l) resume_(col_(j));
      if (!mans && ret) return true;
      ans.pop_back();
    }
    resume_(now);
    return ret;
  }
  constexpr void ins_row_(u32 row, vecu const &cols) {
    assert(row > 0);
    u32 n = (u32)data.size();
    for (u32 i = 0; i < cols.size(); ++i) {
      data.emplace_back(n + i - 1, n + i + 1, u_(cols[i]), cols[i], row, cols[i]);
      u_(cols[i]) = d_(u_(cols[i])) = n + i;
      ++cnt_col[cols[i]];
      if (d_(cols[i]) == cols[i]) d_(cols[i]) = n + i;
    }
    r_(l_(n) = u32(data.size() - 1)) = n;
  }

 public:
  explicit constexpr DLX(vvecb const &grid, bool multi_ans = false) : data(), cnt_col(), mans(multi_ans) {
    u32 col = (u32)grid[0].size();
    assert(col > 0);
    cnt_col.resize(col + 1);
    data.reserve(col + 1);
    for (u32 i = 0; i <= col; ++i) data.emplace_back(i - 1, i + 1, i, i, 0, i);
    r_(l_(0) = col) = 0;
    for (u32 i = 0; i < grid.size(); ++i) {
      vecu _;
      _.reserve(col);
      for (u32 j = 0; j < col; ++j)
        if (grid[i][j]) _.push_back(j + 1);
      _.shrink_to_fit();
      if (!_.empty()) ins_row_(i + 1, _);
    }
  }

  template <class F>
  constexpr std::optional<vecu> dance(F &&cb) {
    vecu ans;
    if (!dance_(ans, std::forward<F>(cb))) return {};
    return ans;
  }

#undef l_
#undef r_
#undef u_
#undef d_
#undef row_
#undef col_
#undef dlxfor_
};

}  // namespace tifa_libs::util

#endif
#line 1 "src/code/util/dlx.hpp"



#line 1 "src/code/util/util.hpp"



#include <bits/stdc++.h>

template <class T>
constexpr T abs(T x) { return x < 0 ? -x : x; }

using i8 = int8_t;
using i16 = int16_t;
using i32 = int32_t;
using i64 = int64_t;
using i128 = __int128_t;
using isz = ptrdiff_t;

using u8 = uint8_t;
using u16 = uint16_t;
using u32 = uint32_t;
using u64 = uint64_t;
using u128 = __uint128_t;
using usz = size_t;

using f32 = float;
using f64 = double;
using f128 = long double;

template <class T>
using ptt = std::pair<T, T>;
template <class T>
using pt3 = std::tuple<T, T, T>;
template <class T>
using pt4 = std::tuple<T, T, T, T>;

template <class T, usz N>
using arr = std::array<T, N>;
template <class T>
using vec = std::vector<T>;
template <class T>
using vvec = vec<vec<T>>;
template <class T>
using v3ec = vec<vvec<T>>;
template <class U, class T>
using vecp = vec<std::pair<U, T>>;
template <class U, class T>
using vvecp = vvec<std::pair<U, T>>;
template <class T>
using vecpt = vec<ptt<T>>;
template <class T>
using vvecpt = vvec<ptt<T>>;

template <class T, class C = std::less<T>>
using pq = std::priority_queue<T, vec<T>, C>;
template <class T>
using pqg = std::priority_queue<T, vec<T>, std::greater<T>>;

using strn = std::string;
using strnv = std::string_view;

using vecu = vec<u32>;
using vvecu = vvec<u32>;
using v3ecu = v3ec<u32>;
using vecu64 = vec<u64>;
using vecb = vec<bool>;
using vvecb = vvec<bool>;

#ifdef ONLINE_JUDGE
#undef assert
#define assert(x) 42
#endif

using namespace std::literals;

constexpr i8 operator""_i8(unsigned long long x) { return (i8)x; }
constexpr i16 operator""_i16(unsigned long long x) { return (i16)x; }
constexpr i32 operator""_i32(unsigned long long x) { return (i32)x; }
constexpr i64 operator""_i64(unsigned long long x) { return (i64)x; }
constexpr isz operator""_iz(unsigned long long x) { return (isz)x; }

constexpr u8 operator""_u8(unsigned long long x) { return (u8)x; }
constexpr u16 operator""_u16(unsigned long long x) { return (u16)x; }
constexpr u32 operator""_u32(unsigned long long x) { return (u32)x; }
constexpr u64 operator""_u64(unsigned long long x) { return (u64)x; }
constexpr usz operator""_uz(unsigned long long x) { return (usz)x; }

inline const auto fn_0 = [](auto&&...) {};


#line 5 "src/code/util/dlx.hpp"

namespace tifa_libs::util {

class DLX {
  struct node {
    u32 l, r, u, d, row, col;
    constexpr node(u32 l = 0, u32 r = 0, u32 u = 0, u32 d = 0, u32 row = 0, u32 col = 0) : l(l), r(r), u(u), d(d), row(row), col(col) {}
  };
  vec<node> data;
  vecu cnt_col;
  const bool mans;

#define l_(x) data[x].l
#define r_(x) data[x].r
#define u_(x) data[x].u
#define d_(x) data[x].d
#define row_(x) data[x].row
#define col_(x) data[x].col
#define dlxfor_(i, l, dir) for (u32 ied__ = (l), i = dir##_(ied__); i != ied__; i = dir##_(i))

  constexpr void remove_(u32 col) {
    r_(l_(col)) = r_(col);
    l_(r_(col)) = l_(col);
    dlxfor_ (i, col, d)
      dlxfor_ (j, i, r) {
        u_(d_(j)) = u_(j);
        d_(u_(j)) = d_(j);
        --cnt_col[col_(j)];
      }
  }
  constexpr void resume_(u32 col) {
    r_(l_(col)) = l_(r_(col)) = col;
    dlxfor_ (i, col, u)
      dlxfor_ (j, i, r) {
        u_(d_(j)) = d_(u_(j)) = j;
        ++cnt_col[col_(j)];
      }
  }
  template <class F>
  constexpr bool dance_(vecu &ans, F &&cb) {
    u32 now = r_(0);
    if (now == 0) return cb(ans), true;
    dlxfor_ (i, 0, r)
      if (cnt_col[i] < cnt_col[now]) now = i;
    remove_(now);
    bool ret = false;
    dlxfor_ (i, now, d) {
      ans.push_back(row_(i));
      dlxfor_ (j, i, r) remove_(col_(j));
      ret |= dance_(ans, std::forward<F>(cb));
      dlxfor_ (j, i, l) resume_(col_(j));
      if (!mans && ret) return true;
      ans.pop_back();
    }
    resume_(now);
    return ret;
  }
  constexpr void ins_row_(u32 row, vecu const &cols) {
    assert(row > 0);
    u32 n = (u32)data.size();
    for (u32 i = 0; i < cols.size(); ++i) {
      data.emplace_back(n + i - 1, n + i + 1, u_(cols[i]), cols[i], row, cols[i]);
      u_(cols[i]) = d_(u_(cols[i])) = n + i;
      ++cnt_col[cols[i]];
      if (d_(cols[i]) == cols[i]) d_(cols[i]) = n + i;
    }
    r_(l_(n) = u32(data.size() - 1)) = n;
  }

 public:
  explicit constexpr DLX(vvecb const &grid, bool multi_ans = false) : data(), cnt_col(), mans(multi_ans) {
    u32 col = (u32)grid[0].size();
    assert(col > 0);
    cnt_col.resize(col + 1);
    data.reserve(col + 1);
    for (u32 i = 0; i <= col; ++i) data.emplace_back(i - 1, i + 1, i, i, 0, i);
    r_(l_(0) = col) = 0;
    for (u32 i = 0; i < grid.size(); ++i) {
      vecu _;
      _.reserve(col);
      for (u32 j = 0; j < col; ++j)
        if (grid[i][j]) _.push_back(j + 1);
      _.shrink_to_fit();
      if (!_.empty()) ins_row_(i + 1, _);
    }
  }

  template <class F>
  constexpr std::optional<vecu> dance(F &&cb) {
    vecu ans;
    if (!dance_(ans, std::forward<F>(cb))) return {};
    return ans;
  }

#undef l_
#undef r_
#undef u_
#undef d_
#undef row_
#undef col_
#undef dlxfor_
};

}  // namespace tifa_libs::util


Back to top page