Problem p02561 (C++) — buggy submission, verdict: Runtime Error

#pragma region header
#include <bits/stdc++.h>
#define int long long
#define all(a) begin(a), end(a)
#define rall(a) rbegin(a), rend(a)
#define mp make_pair
#define rep1(i, n) for (decltype(+n) i = 0; i < (n); i++)
#define rrep1(i, n) for (auto i = n - 1; i > static_cast<decltype(i)>(-1); i--)
#define rep2(i, a, b) for (auto i = (a); i < (b); i++)
#define rrep2(i, a, b) for (auto i = b - 1; i >= a; i--)
#define GET_MACRO(_1, _2, _3, NAME, ...) NAME
#define rep(...) GET_MACRO(__VA_ARGS__, rep2, rep1)(__VA_ARGS__)
#define rrep(...) GET_MACRO(__VA_ARGS__, rrep2, rrep1)(__VA_ARGS__)
#define each(i, a) for (auto &&i : (a))
using namespace std;

using ld = long double;
using vi = vector<int>;
using vvi = vector<vi>;
using vs = vector<string>;
using vvs = vector<vs>;
using vd = vector<ld>;
using vvd = vector<vd>;
using vb = vector<bool>;
using vvb = vector<vb>;
using pii = pair<int, int>;
using vp = vector<pii>;
using vvp = vector<vp>;
using mii = map<int, int>;
using vm = vector<mii>;
using vvm = vector<vm>;
template <class T, class U> using umap = unordered_map<T, U>;
using umii = umap<int, int>;
using seti = set<int>;
template <class T> using uset = unordered_set<T>;
using useti = uset<int>;
template <class T> using less_queue = priority_queue<T>;
template <class T>
using greater_queue = priority_queue<T, vector<T>, greater<T>>;
using int128 = __int128_t;

ostream &operator<<(ostream &dest, int128 value) {
  ostream::sentry s(dest);
  if (s) {
    int128 tmp = value < 0 ? -value : value;
    char buffer[128];
    char *d = end(buffer);
    do {
      --d;
      *d = "0123456789"[tmp % 10];
      tmp /= 10;
    } while (tmp != 0);
    if (value < 0) {
      --d;
      *d = '-';
    }
    int len = end(buffer) - d;
    if (dest.rdbuf()->sputn(d, len) != len) {
      dest.setstate(ios_base::badbit);
    }
  }
  return dest;
}

const int INF = 1e18;
const ld EPS = 1e-10;

template <class T> void SORT(T &a) { stable_sort(all(a)); }
template <class T> void RSORT(T &a) { stable_sort(rall(a)); }
template <class T> void rev(T &a) { reverse(all(a)); }
template <class T> void uniq(T &a) { a.erase(unique(all(a)), end(a)); }
template <class T> auto min_of(const T &a) { return *min_element(all(a)); }
template <class T> auto max_of(const T &a) { return *max_element(all(a)); }
template <class T> T sum_of(const vector<T> &a) {
  return accumulate(all(a), (T)0);
}
template <class T, class U> int count_of(const T &a, const U &i) {
  return count(all(a), i);
}
template <class T> bool has(const vector<T> &a, const T &i) {
  return find(all(a), i) != a.end();
}
bool has(const string &a, const char &i) { return find(all(a), i) != a.end(); }
template <class T> bool has(const set<T> &a, const T &i) {
  return a.find(i) != a.end();
}
template <class T, class U> bool has(const map<T, U> &a, const T &i) {
  return a.find(i) != a.end();
}
template <class T, class U> bool has(const umap<T, U> &a, const T &i) {
  return a.find(i) != a.end();
}
template <class T> int sz(const T &a) { return a.size(); };

template <class T> void COUT(const T &x) { cout << x << endl; }
template <class T, class U> void COUT(const T &x, const U &y) {
  cout << x << ' ' << y << endl;
}
template <class T, class U, class V>
void COUT(const T &x, const U &y, const V &z) {
  cout << x << ' ' << y << ' ' << z << endl;
}
template <class T> void CSP(const T &x) { cout << x << ' '; }
template <class T> void CVEC(const T &v) {
  int c = v.size() - 1;
  for (int i = 0; i < c; i++)
    cout << v[i] << ' ';
  if (c > -1)
    cout << v[c];
  cout << endl;
}

template <class T> bool amin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool amax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

constexpr int lshift(const int &x) noexcept { return 1ll << x; }
constexpr int popcount(const unsigned int &x) noexcept {
  return __builtin_popcountll(x);
}
constexpr int least1(const unsigned int &x) noexcept {
  return __builtin_ffsll(x);
}
constexpr int ceil_div(const int &x, const int &y) noexcept {
  return (x + y - 1) / y;
}

#pragma endregion header

template <class Cap> struct mf_graph {
public:
  mf_graph() : _n(0) {}
  mf_graph(int n) : _n(n), g(n) {}

  int add_edge(int from, int to, Cap cap) {
    assert(0 <= from && from < _n);
    assert(0 <= to && to < _n);
    assert(0 <= cap);
    int m = (int)(pos.size());
    pos.push_back({from, (int)(g[from].size())});
    g[from].push_back(_edge{to, (int)(g[to].size()), cap});
    g[to].push_back(_edge{from, (int)(g[from].size()) - 1, 0});
    return m;
  }

  struct edge {
    int from, to;
    Cap cap, flow;
  };

  edge get_edge(int i) {
    int m = (int)(pos.size());
    assert(0 <= i && i < m);
    auto _e = g[pos[i].first][pos[i].second];
    auto _re = g[_e.to][_e.rev];
    return edge{pos[i].first, _e.to, _e.cap + _re.cap, _re.cap};
  }
  vector<edge> edges() {
    int m = (int)(pos.size());
    vector<edge> result;
    for (int i = 0; i < m; i++) {
      result.push_back(get_edge(i));
    }
    return result;
  }
  void change_edge(int i, Cap new_cap, Cap new_flow) {
    int m = (int)(pos.size());
    assert(0 <= i && i < m);
    assert(0 <= new_flow && new_flow <= new_cap);
    auto &_e = g[pos[i].first][pos[i].second];
    auto &_re = g[_e.to][_e.rev];
    _e.cap = new_cap - new_flow;
    _re.cap = new_flow;
  }

  Cap flow(int s, int t) { return flow(s, t, numeric_limits<Cap>::max()); }
  Cap flow(int s, int t, Cap flow_limit) {
    assert(0 <= s && s < _n);
    assert(0 <= t && t < _n);

    vector<int> level(_n), iter(_n);
    simple_queue<int> que;

    auto bfs = [&]() {
      fill(level.begin(), level.end(), -1);
      level[s] = 0;
      que.clear();
      que.push(s);
      while (!que.empty()) {
        int v = que.front();
        que.pop();
        for (auto e : g[v]) {
          if (e.cap == 0 || level[e.to] >= 0)
            continue;
          level[e.to] = level[v] + 1;
          if (e.to == t)
            return;
          que.push(e.to);
        }
      }
    };
    auto dfs = [&](auto self, int v, Cap up) {
      if (v == s)
        return up;
      Cap res = 0;
      int level_v = level[v];
      for (int &i = iter[v]; i < (int)(g[v].size()); i++) {
        _edge &e = g[v][i];
        if (level_v <= level[e.to] || g[e.to][e.rev].cap == 0)
          continue;
        Cap d = self(self, e.to, min(up - res, g[e.to][e.rev].cap));
        if (d <= 0)
          continue;
        g[v][i].cap += d;
        g[e.to][e.rev].cap -= d;
        res += d;
        if (res == up)
          break;
      }
      return res;
    };

    Cap flow = 0;
    while (flow < flow_limit) {
      bfs();
      if (level[t] == -1)
        break;
      fill(iter.begin(), iter.end(), 0);
      while (flow < flow_limit) {
        Cap f = dfs(dfs, t, flow_limit - flow);
        if (!f)
          break;
        flow += f;
      }
    }
    return flow;
  }

  vector<bool> min_cut(int s) {
    vector<bool> visited(_n);
    simple_queue<int> que;
    que.push(s);
    while (!que.empty()) {
      int p = que.front();
      que.pop();
      visited[p] = true;
      for (auto e : g[p]) {
        if (e.cap && !visited[e.to]) {
          visited[e.to] = true;
          que.push(e.to);
        }
      }
    }
    return visited;
  }

private:
  int _n;
  struct _edge {
    int to, rev;
    Cap cap;
  };
  vector<pair<int, int>> pos;
  vector<vector<_edge>> g;
  template <class T> struct simple_queue {
    vector<T> payload;
    int pos = 0;
    void reserve(int n) { payload.reserve(n); }
    int size() const { return (int)(payload.size()) - pos; }
    bool empty() const { return pos == (int)(payload.size()); }
    void push(const T &t) { payload.push_back(t); }
    T &front() { return payload[pos]; }
    void clear() {
      payload.clear();
      pos = 0;
    }
    void pop() { pos++; }
  };
};
// mf_graph<int> mf(N);

const int nxv[] = {0, 1, 0, -1}, nyv[] = {1, 0, -1, 0};

void solve(int N, int M, vector<string> S) {
  mf_graph<int> mf(N * M + 2);
  int s = N * M, t = s + 1;
  rep(i, N) rep(j, M) {
    if (S[i][j] == '#')
      continue;
    int k = i * M + j;
    if ((i + j) % 2)
      mf.add_edge(s, k, 1);
    else
      mf.add_edge(k, t, 1);
    if ((i + j) % 2)
      rep(ni, 4) {
        int nx = i + nxv[ni], ny = j + nyv[ni];
        if (nx < 0 || ny < 0 || nx >= N || ny >= M)
          continue;
        if (S[nx][ny] == '#')
          continue;
        mf.add_edge(k, nx * M + ny, 1);
      }
  }
  COUT(mf.flow(s, t));
  each(e, mf.edges()) {
    if (e.from == s || e.to == t || e.flow == 0)
      continue;
    int u = e.from, v = e.to;
    if (u > v)
      swap(u, v);
    int ux = u / M, uy = u % M;
    int vx = v / M, vy = v % M;
    if (M > 1 && v - u == 1) {
      S[ux][uy] = '>';
      S[vx][vy] = '<';
    } else {
      S[ux][uy] = 'v';
      S[vx][vy] = '^';
    }
  }
  each(x, S) COUT(x);
}

#pragma region main
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(15);

  int N;
  cin >> N;
  int M;
  cin >> M;
  vector<string> S(N);
  for (int i = 0; i < N; i++) {
    cin >> S[i];
  }
  solve(N, M, move(S));
}
#pragma endregion main


Error: WA

Fixed submission:

#pragma region header
#include <bits/stdc++.h>
#define int long long
#define all(a) begin(a), end(a)
#define rall(a) rbegin(a), rend(a)
#define mp make_pair
#define rep1(i, n) for (decltype(+n) i = 0; i < (n); i++)
#define rrep1(i, n) for (auto i = n - 1; i > static_cast<decltype(i)>(-1); i--)
#define rep2(i, a, b) for (auto i = (a); i < (b); i++)
#define rrep2(i, a, b) for (auto i = b - 1; i >= a; i--)
#define GET_MACRO(_1, _2, _3, NAME, ...) NAME
#define rep(...) GET_MACRO(__VA_ARGS__, rep2, rep1)(__VA_ARGS__)
#define rrep(...) GET_MACRO(__VA_ARGS__, rrep2, rrep1)(__VA_ARGS__)
#define each(i, a) for (auto &&i : (a))
using namespace std;

using ld = long double;
using vi = vector<int>;
using vvi = vector<vi>;
using vs = vector<string>;
using vvs = vector<vs>;
using vd = vector<ld>;
using vvd = vector<vd>;
using vb = vector<bool>;
using vvb = vector<vb>;
using pii = pair<int, int>;
using vp = vector<pii>;
using vvp = vector<vp>;
using mii = map<int, int>;
using vm = vector<mii>;
using vvm = vector<vm>;
template <class T, class U> using umap = unordered_map<T, U>;
using umii = umap<int, int>;
using seti = set<int>;
template <class T> using uset = unordered_set<T>;
using useti = uset<int>;
template <class T> using less_queue = priority_queue<T>;
template <class T>
using greater_queue = priority_queue<T, vector<T>, greater<T>>;
using int128 = __int128_t;

ostream &operator<<(ostream &dest, int128 value) {
  ostream::sentry s(dest);
  if (s) {
    int128 tmp = value < 0 ? -value : value;
    char buffer[128];
    char *d = end(buffer);
    do {
      --d;
      *d = "0123456789"[tmp % 10];
      tmp /= 10;
    } while (tmp != 0);
    if (value < 0) {
      --d;
      *d = '-';
    }
    int len = end(buffer) - d;
    if (dest.rdbuf()->sputn(d, len) != len) {
      dest.setstate(ios_base::badbit);
    }
  }
  return dest;
}

const int INF = 1e18;
const ld EPS = 1e-10;

template <class T> void SORT(T &a) { stable_sort(all(a)); }
template <class T> void RSORT(T &a) { stable_sort(rall(a)); }
template <class T> void rev(T &a) { reverse(all(a)); }
template <class T> void uniq(T &a) { a.erase(unique(all(a)), end(a)); }
template <class T> auto min_of(const T &a) { return *min_element(all(a)); }
template <class T> auto max_of(const T &a) { return *max_element(all(a)); }
template <class T> T sum_of(const vector<T> &a) {
  return accumulate(all(a), (T)0);
}
template <class T, class U> int count_of(const T &a, const U &i) {
  return count(all(a), i);
}
template <class T> bool has(const vector<T> &a, const T &i) {
  return find(all(a), i) != a.end();
}
bool has(const string &a, const char &i) { return find(all(a), i) != a.end(); }
template <class T> bool has(const set<T> &a, const T &i) {
  return a.find(i) != a.end();
}
template <class T, class U> bool has(const map<T, U> &a, const T &i) {
  return a.find(i) != a.end();
}
template <class T, class U> bool has(const umap<T, U> &a, const T &i) {
  return a.find(i) != a.end();
}
template <class T> int sz(const T &a) { return a.size(); };

template <class T> void COUT(const T &x) { cout << x << endl; }
template <class T, class U> void COUT(const T &x, const U &y) {
  cout << x << ' ' << y << endl;
}
template <class T, class U, class V>
void COUT(const T &x, const U &y, const V &z) {
  cout << x << ' ' << y << ' ' << z << endl;
}
template <class T> void CSP(const T &x) { cout << x << ' '; }
template <class T> void CVEC(const T &v) {
  int c = v.size() - 1;
  for (int i = 0; i < c; i++)
    cout << v[i] << ' ';
  if (c > -1)
    cout << v[c];
  cout << endl;
}

template <class T> bool amin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool amax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

constexpr int lshift(const int &x) noexcept { return 1ll << x; }
constexpr int popcount(const unsigned int &x) noexcept {
  return __builtin_popcountll(x);
}
constexpr int least1(const unsigned int &x) noexcept {
  return __builtin_ffsll(x);
}
constexpr int ceil_div(const int &x, const int &y) noexcept {
  return (x + y - 1) / y;
}

#pragma endregion header

template <class Cap> struct mf_graph {
public:
  mf_graph() : _n(0) {}
  mf_graph(int n) : _n(n), g(n) {}

  int add_edge(int from, int to, Cap cap) {
    assert(0 <= from && from < _n);
    assert(0 <= to && to < _n);
    assert(0 <= cap);
    int m = (int)(pos.size());
    pos.push_back({from, (int)(g[from].size())});
    g[from].push_back(_edge{to, (int)(g[to].size()), cap});
    g[to].push_back(_edge{from, (int)(g[from].size()) - 1, 0});
    return m;
  }

  struct edge {
    int from, to;
    Cap cap, flow;
  };

  edge get_edge(int i) {
    int m = (int)(pos.size());
    assert(0 <= i && i < m);
    auto _e = g[pos[i].first][pos[i].second];
    auto _re = g[_e.to][_e.rev];
    return edge{pos[i].first, _e.to, _e.cap + _re.cap, _re.cap};
  }
  vector<edge> edges() {
    int m = (int)(pos.size());
    vector<edge> result;
    for (int i = 0; i < m; i++) {
      result.push_back(get_edge(i));
    }
    return result;
  }
  void change_edge(int i, Cap new_cap, Cap new_flow) {
    int m = (int)(pos.size());
    assert(0 <= i && i < m);
    assert(0 <= new_flow && new_flow <= new_cap);
    auto &_e = g[pos[i].first][pos[i].second];
    auto &_re = g[_e.to][_e.rev];
    _e.cap = new_cap - new_flow;
    _re.cap = new_flow;
  }

  Cap flow(int s, int t) { return flow(s, t, numeric_limits<Cap>::max()); }
  Cap flow(int s, int t, Cap flow_limit) {
    assert(0 <= s && s < _n);
    assert(0 <= t && t < _n);

    vector<int> level(_n), iter(_n);
    simple_queue<int> que;

    auto bfs = [&]() {
      fill(level.begin(), level.end(), -1);
      level[s] = 0;
      que.clear();
      que.push(s);
      while (!que.empty()) {
        int v = que.front();
        que.pop();
        for (auto e : g[v]) {
          if (e.cap == 0 || level[e.to] >= 0)
            continue;
          level[e.to] = level[v] + 1;
          if (e.to == t)
            return;
          que.push(e.to);
        }
      }
    };
    auto dfs = [&](auto self, int v, Cap up) {
      if (v == s)
        return up;
      Cap res = 0;
      int level_v = level[v];
      for (int &i = iter[v]; i < (int)(g[v].size()); i++) {
        _edge &e = g[v][i];
        if (level_v <= level[e.to] || g[e.to][e.rev].cap == 0)
          continue;
        Cap d = self(self, e.to, min(up - res, g[e.to][e.rev].cap));
        if (d <= 0)
          continue;
        g[v][i].cap += d;
        g[e.to][e.rev].cap -= d;
        res += d;
        if (res == up)
          break;
      }
      return res;
    };

    Cap flow = 0;
    while (flow < flow_limit) {
      bfs();
      if (level[t] == -1)
        break;
      fill(iter.begin(), iter.end(), 0);
      while (flow < flow_limit) {
        Cap f = dfs(dfs, t, flow_limit - flow);
        if (!f)
          break;
        flow += f;
      }
    }
    return flow;
  }

  vector<bool> min_cut(int s) {
    vector<bool> visited(_n);
    simple_queue<int> que;
    que.push(s);
    while (!que.empty()) {
      int p = que.front();
      que.pop();
      visited[p] = true;
      for (auto e : g[p]) {
        if (e.cap && !visited[e.to]) {
          visited[e.to] = true;
          que.push(e.to);
        }
      }
    }
    return visited;
  }

private:
  int _n;
  struct _edge {
    int to, rev;
    Cap cap;
  };
  vector<pair<int, int>> pos;
  vector<vector<_edge>> g;
  template <class T> struct simple_queue {
    vector<T> payload;
    int pos = 0;
    void reserve(int n) { payload.reserve(n); }
    int size() const { return (int)(payload.size()) - pos; }
    bool empty() const { return pos == (int)(payload.size()); }
    void push(const T &t) { payload.push_back(t); }
    T &front() { return payload[pos]; }
    void clear() {
      payload.clear();
      pos = 0;
    }
    void pop() { pos++; }
  };
};
// mf_graph<int> mf(N);

const int vx[] = {0, 1, 0, -1}, vy[] = {1, 0, -1, 0};

void solve(int N, int M, vector<string> S) {
  mf_graph<int> mf(N * M + 2);
  int s = N * M, t = s + 1;
  rep(i, N) rep(j, M) {
    if (S[i][j] == '#')
      continue;
    int k = i * N + j;
    if (k % 2)
      mf.add_edge(s, k, 1);
    else
      mf.add_edge(k, t, 1);
    rep(ni, 4) {
      int nx = i + vx[ni], ny = j + vy[ni];
      if (nx < 0 || ny < 0 || nx >= N || ny >= M)
        continue;
      if (S[nx][ny] == '#')
        continue;
      int nk = nx * N + ny;
      if (k % 2)
        mf.add_edge(k, nk, 1);
      else
        mf.add_edge(nk, k, 1);
    }
  }
  COUT(mf.flow(s, t));
  each(e, mf.edges()) {
    if (e.from == s || e.to == t || e.flow == 0)
      continue;
    int u = e.from, v = e.to;
    if (u > v)
      swap(u, v);
    int ux = u / N, uy = u % N;
    int vx = v / N, vy = v % N;
    if (v - u == 1) {
      S[ux][uy] = '>';
      S[vx][vy] = '<';
    } else {
      S[ux][uy] = 'v';
      S[vx][vy] = '^';
    }
  }
  each(x, S) COUT(x);
}

#pragma region main
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(15);

  int N;
  cin >> N;
  int M;
  cin >> M;
  vector<string> S(N);
  for (int i = 0; i < N; i++) {
    cin >> S[i];
  }
  solve(N, M, move(S));
}
#pragma endregion main