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

#line 1 "/workspaces/compro/lib/atcoder/maxflow.hpp"

#line 1 "/workspaces/compro/lib/atcoder/internal_queue.hpp"

#include <vector>

namespace atcoder {

namespace internal {

template <class T> struct simple_queue {
  std::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++; }
};

} // namespace internal

} // namespace atcoder

#line 5 "/workspaces/compro/lib/atcoder/maxflow.hpp"
#include <algorithm>
#include <cassert>
#include <limits>
#include <queue>
#line 10 "/workspaces/compro/lib/atcoder/maxflow.hpp"

namespace atcoder {

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())});
    int from_id = int(g[from].size());
    int to_id = int(g[to].size());
    if (from == to)
      to_id++;
    g[from].push_back(_edge{to, to_id, cap});
    g[to].push_back(_edge{from, from_id, 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};
  }
  std::vector<edge> edges() {
    int m = int(pos.size());
    std::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, std::numeric_limits<Cap>::max()); }
  Cap flow(int s, int t, Cap flow_limit) {
    assert(0 <= s && s < _n);
    assert(0 <= t && t < _n);
    assert(s != t);

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

    auto bfs = [&]() {
      std::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, std::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;
      std::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;
  }

  std::vector<bool> min_cut(int s) {
    std::vector<bool> visited(_n);
    internal::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;
  };
  std::vector<std::pair<int, int>> pos;
  std::vector<std::vector<_edge>> g;
};

} // namespace atcoder

#line 1 "/workspaces/compro/lib/template.hpp"

#line 1 "/workspaces/compro/lib/io/vector.hpp"
#include <iostream>
#line 3 "/workspaces/compro/lib/io/vector.hpp"

#ifndef IO_VECTOR
#define IO_VECTOR

template <class T>
std::ostream &operator<<(std::ostream &out, const std::vector<T> &v) {
  int size = v.size();
  for (int i = 0; i < size; i++) {
    std::cout << v[i];
    if (i != size - 1)
      std::cout << " ";
  }
  return out;
}

template <class T>
std::istream &operator>>(std::istream &in, std::vector<T> &v) {
  for (auto &el : v) {
    std::cin >> el;
  }
  return in;
}

#endif
#line 4 "/workspaces/compro/lib/template.hpp"
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) (v).begin(), (v).end()
#define coutd(n) cout << fixed << setprecision(n)
#define ll long long int
#define vl vector<ll>
#define vi vector<int>
#define MM << " " <<

using namespace std;

template <class T> void say(bool val, T yes, T no) {
  cout << (val ? yes : no) << "\\n";
}

void say(bool val, string yes = "Yes", string no = "No") {
  say<string>(val, yes, no);
}

template <class T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}

template <class T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}

// C++ 17に完全移行したら消す
// 最大公約数を求める
template <class T> T gcd(T n, T m) { return n ? gcd(m % n, n) : m; }

// 最小公倍数を求める
template <class T> T lcm(T n, T m) {
  int g = gcd(n, m);
  return n * m / g;
}

// 重複を消す。計算量はO(NlogN)
template <class T> void unique(std::vector<T> &v) {
  std::sort(v.begin(), v.end());
  v.erase(std::unique(v.begin(), v.end()), v.end());
}

#line 3 "main.cpp"

// generated by online-judge-template-generator v4.4.0
// (https://github.com/kmyk/online-judge-template-generator)
int main() {
  long long N;
  long long M;
  std::cin >> N;
  std::vector<std::string> S(N);
  std::cin >> M;
  for (int i = 0; i < N; ++i) {
    std::cin >> S[i];
  }

  atcoder::mf_graph<int> g(N * M + 2);

  auto edge_index = [&](int i, int j) { return i * M + j; };

  int s = N * M;
  int t = s + 1;

  int di[4] = {1, 0, -1, 0};
  int dj[4] = {0, 1, 0, -1};
  REP(i, N) {
    REP(j, M) {
      if (S[i][j] == '#')
        continue;
      if ((i + j) % 2 == 0) {
        g.add_edge(s, edge_index(i, j), 1);
        REP(k, 4) {
          int ni = i + di[k], nj = j + dj[k];
          if (0 <= ni && ni < N && 0 <= nj && nj < M && S[ni][nj] != '#') {
            g.add_edge(edge_index(i, j), edge_index(ni, nj), 1);
          }
        }
      } else {
        g.add_edge(edge_index(i, j), t, 1);
      }
    }
  }

  int ans = g.flow(s, t);
  cout << ans << endl;

  auto edges = g.edges();
  auto rev = [&](int idx) -> pair<int, int> { return {idx / M, idx % M}; };
  for (const auto e : edges) {
    if (e.from == s || e.to == t || e.flow == 0)
      continue;
    auto [si, sj] = rev(e.from);
    auto [ti, tj] = rev(e.to);
    if (si == ti + 1) {
      S[ti][tj] = 'v';
      S[si][sj] = '^';
    } else if (si + 1 == ti) {
      S[si][sj] = 'v';
      S[ti][tj] = '^';
    } else if (sj == tj + 1) {
      S[si][sj] = '<';
      S[ti][tj] = '>';
    } else {
      S[ti][tj] = '<';
      S[si][sj] = '>';
    }
  }

  REP(i, N) { cout << S[i] << endl; }

  return 0;
}


Error: WA

Fixed submission:

#line 1 "/workspaces/compro/lib/atcoder/maxflow.hpp"

#line 1 "/workspaces/compro/lib/atcoder/internal_queue.hpp"

#include <vector>

namespace atcoder {

namespace internal {

template <class T> struct simple_queue {
  std::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++; }
};

} // namespace internal

} // namespace atcoder

#line 5 "/workspaces/compro/lib/atcoder/maxflow.hpp"
#include <algorithm>
#include <cassert>
#include <limits>
#include <queue>
#line 10 "/workspaces/compro/lib/atcoder/maxflow.hpp"

namespace atcoder {

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())});
    int from_id = int(g[from].size());
    int to_id = int(g[to].size());
    if (from == to)
      to_id++;
    g[from].push_back(_edge{to, to_id, cap});
    g[to].push_back(_edge{from, from_id, 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};
  }
  std::vector<edge> edges() {
    int m = int(pos.size());
    std::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, std::numeric_limits<Cap>::max()); }
  Cap flow(int s, int t, Cap flow_limit) {
    assert(0 <= s && s < _n);
    assert(0 <= t && t < _n);
    assert(s != t);

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

    auto bfs = [&]() {
      std::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, std::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;
      std::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;
  }

  std::vector<bool> min_cut(int s) {
    std::vector<bool> visited(_n);
    internal::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;
  };
  std::vector<std::pair<int, int>> pos;
  std::vector<std::vector<_edge>> g;
};

} // namespace atcoder

#line 1 "/workspaces/compro/lib/template.hpp"

#line 1 "/workspaces/compro/lib/io/vector.hpp"
#include <iostream>
#line 3 "/workspaces/compro/lib/io/vector.hpp"

#ifndef IO_VECTOR
#define IO_VECTOR

template <class T>
std::ostream &operator<<(std::ostream &out, const std::vector<T> &v) {
  int size = v.size();
  for (int i = 0; i < size; i++) {
    std::cout << v[i];
    if (i != size - 1)
      std::cout << " ";
  }
  return out;
}

template <class T>
std::istream &operator>>(std::istream &in, std::vector<T> &v) {
  for (auto &el : v) {
    std::cin >> el;
  }
  return in;
}

#endif
#line 4 "/workspaces/compro/lib/template.hpp"
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) (v).begin(), (v).end()
#define coutd(n) cout << fixed << setprecision(n)
#define ll long long int
#define vl vector<ll>
#define vi vector<int>
#define MM << " " <<

using namespace std;

template <class T> void say(bool val, T yes, T no) {
  cout << (val ? yes : no) << "\\n";
}

void say(bool val, string yes = "Yes", string no = "No") {
  say<string>(val, yes, no);
}

template <class T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}

template <class T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}

// C++ 17に完全移行したら消す
// 最大公約数を求める
template <class T> T gcd(T n, T m) { return n ? gcd(m % n, n) : m; }

// 最小公倍数を求める
template <class T> T lcm(T n, T m) {
  int g = gcd(n, m);
  return n * m / g;
}

// 重複を消す。計算量はO(NlogN)
template <class T> void unique(std::vector<T> &v) {
  std::sort(v.begin(), v.end());
  v.erase(std::unique(v.begin(), v.end()), v.end());
}

#line 3 "main.cpp"

// generated by online-judge-template-generator v4.4.0
// (https://github.com/kmyk/online-judge-template-generator)
int main() {
  long long N;
  long long M;
  std::cin >> N;
  std::vector<std::string> S(N);
  std::cin >> M;
  for (int i = 0; i < N; ++i) {
    std::cin >> S[i];
  }

  atcoder::mf_graph<int> g(N * M + 2);

  auto edge_index = [&](int i, int j) { return i * M + j; };

  int s = N * M;
  int t = s + 1;

  int di[4] = {1, 0, -1, 0};
  int dj[4] = {0, 1, 0, -1};
  REP(i, N) {
    REP(j, M) {
      if (S[i][j] == '#')
        continue;
      if ((i + j) % 2 == 0) {
        g.add_edge(s, edge_index(i, j), 1);
        REP(k, 4) {
          int ni = i + di[k], nj = j + dj[k];
          if (0 <= ni && ni < N && 0 <= nj && nj < M && S[ni][nj] != '#') {
            g.add_edge(edge_index(i, j), edge_index(ni, nj), 1);
          }
        }
      } else {
        g.add_edge(edge_index(i, j), t, 1);
      }
    }
  }

  int ans = g.flow(s, t);
  cout << ans << endl;

  auto edges = g.edges();
  auto rev = [&](int idx) -> pair<int, int> { return {idx / M, idx % M}; };
  for (const auto e : edges) {
    if (e.from == s || e.to == t || e.flow == 0)
      continue;
    auto [si, sj] = rev(e.from);
    auto [ti, tj] = rev(e.to);
    if (si == ti + 1) {
      S[ti][tj] = 'v';
      S[si][ti] = '^';
    } else if (si + 1 == ti) {
      S[si][ti] = 'v';
      S[ti][tj] = '^';
    } else if (sj == tj + 1) {
      S[si][sj] = '<';
      S[ti][tj] = '>';
    } else {
      S[ti][tj] = '<';
      S[si][sj] = '>';
    }
  }

  REP(i, N) { cout << S[i] << endl; }

  return 0;
}