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

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
// #include <atcoder/all>
// using namespace atcoder;
using ll = long long;
#define pp pair<int, int>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define ld long double
#define al(a) (a).begin(), (a).end()
#define mk make_pair
#define check cout << "?" << endl;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  if (v.empty()) {
    os << "{ }";
    return os;
  }
  os << "{" << v.front();
  for (auto itr = ++v.begin(); itr != v.end(); itr++) {
    os << ", " << *itr;
  }
  os << "}";
  return os;
}

ll MOD = 1000000007;
ll mod = 998244353;
int inf = 1000001000;
ll INF = 1e18 + 5;

// 辺edge->e : 逆辺edge->path[e.to][e.rev]
struct edge {
  int to, cap, rev;
};

vector<vector<edge>> path;
vector<int> visited;

void add_edge(int from, int to, int cap) {
  path[from].push_back((edge){to, cap, (int)path[to].size()});
  path[to].push_back((edge){from, 0, (int)path[from].size() - 1});
}

int dfs(int v, int t, int f) {
  if (v == t)
    return f;
  visited[v] = 1;
  for (edge &e : path[v]) {
    if (visited[e.to] == 0 && e.cap > 0) {
      int d = dfs(e.to, t, min(f, e.cap));
      if (d > 0) {
        e.cap -= d;
        path[e.to][e.rev].cap += d;
        return d;
      }
    }
  }
  return 0;
}

int max_flow(int s, int t) {
  int flow = 0;
  while (1) {
    visited.assign(visited.size(), 0);
    int f = dfs(s, t, inf);
    if (f == 0) {
      return flow;
    }
    flow += f;
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<string> s(n);
  rep(i, n) cin >> s[i];
  path.assign(n * m + 2, vector<edge>(0));
  visited.assign(n * m + 2, false);
  rep(i, n) rep(j, m) {
    if ((i + j) % 2) {
      if (s[i][j] == '.')
        add_edge(i * m + j + 1, n * m + 1, 1);
      continue;
    }
    if (i > 0 && s[i - 1][j] == '.' && s[i][j] == '.')
      add_edge(i * m + j + 1, (i - 1) * m + j + 1, 1);
    if (i < n - 1 && s[i + 1][j] == '.' && s[i][j] == '.')
      add_edge(i * m + j + 1, (i + 1) * m + j + 1, 1);
    if (j > 0 && s[i][j - 1] == '.' && s[i][j] == '.')
      add_edge(i * m + j + 1, i * m + j, 1);
    if (j < m - 1 && s[i][j + 1] == '.' && s[i][j] == '.')
      add_edge(i * m + j + 1, i * m + j + 2, 1);

    if (s[i][j] == '.')
      add_edge(0, i * m + j + 1, 1);
  }
  cout << max_flow(0, n * m + 1) << endl;
  int xi, yi;
  rep(i, n) rep(j, m) {
    if ((i + j) % 2)
      continue;
    for (auto e : path[i * m + j + 1]) {
      if (e.to > 0 && e.cap == 0) {
        xi = (e.to - 1) / m;
        yi = (e.to - 1) % m;
        if (xi - i == 1 && yi - j == 0) {
          s[i][j] = 'v';
          s[xi][yi] = '^';
        }
        if (xi - i == -1 && yi - j == 0) {
          s[i][j] = '^';
          s[xi][yi] = 'v';
        }
        if (xi - i == 0 && yi - j == 1) {
          s[i][j] = '>';
          s[xi][yi] = '<';
        }
        if (xi - i == 0 && yi - j == -1) {
          s[i][j] = '<';
          s[xi][yi] = '>';
        }
        break;
      }
    }
  }
  rep(i, n) cout << s[i] << endl;
}

Error: 0

Fixed submission:

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
// #include <atcoder/all>
// using namespace atcoder;
using ll = long long;
#define pp pair<int, int>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define ld long double
#define al(a) (a).begin(), (a).end()
#define mk make_pair
#define check cout << "?" << endl;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  if (v.empty()) {
    os << "{ }";
    return os;
  }
  os << "{" << v.front();
  for (auto itr = ++v.begin(); itr != v.end(); itr++) {
    os << ", " << *itr;
  }
  os << "}";
  return os;
}

ll MOD = 1000000007;
ll mod = 998244353;
int inf = 1000001000;
ll INF = 1e18 + 5;

// 辺edge->e : 逆辺edge->path[e.to][e.rev]
struct edge {
  int to, cap, rev;
};

vector<vector<edge>> path;
vector<int> visited;

void add_edge(int from, int to, int cap) {
  path[from].push_back((edge){to, cap, (int)path[to].size()});
  path[to].push_back((edge){from, 0, (int)path[from].size() - 1});
}

int dfs(int v, int t, int f) {
  if (v == t)
    return f;
  visited[v] = 1;
  for (edge &e : path[v]) {
    if (visited[e.to] == 0 && e.cap > 0) {
      int d = dfs(e.to, t, min(f, e.cap));
      if (d > 0) {
        e.cap -= d;
        path[e.to][e.rev].cap += d;
        return d;
      }
    }
  }
  return 0;
}

int max_flow(int s, int t) {
  int flow = 0;
  while (1) {
    visited.assign(visited.size(), 0);
    int f = dfs(s, t, inf);
    if (f == 0) {
      return flow;
    }
    flow += f;
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<string> s(n);
  rep(i, n) cin >> s[i];
  path.assign(n * m + 2, vector<edge>(0));
  visited.assign(n * m + 2, false);
  rep(i, n) rep(j, m) {
    if ((i + j) % 2) {
      if (s[i][j] == '.')
        add_edge(i * m + j + 1, n * m + 1, 1);
      continue;
    }
    if (i > 0 && s[i - 1][j] == '.' && s[i][j] == '.')
      add_edge(i * m + j + 1, (i - 1) * m + j + 1, 1);
    if (i < n - 1 && s[i + 1][j] == '.' && s[i][j] == '.')
      add_edge(i * m + j + 1, (i + 1) * m + j + 1, 1);
    if (j > 0 && s[i][j - 1] == '.' && s[i][j] == '.')
      add_edge(i * m + j + 1, i * m + j, 1);
    if (j < m - 1 && s[i][j + 1] == '.' && s[i][j] == '.')
      add_edge(i * m + j + 1, i * m + j + 2, 1);

    if (s[i][j] == '.')
      add_edge(0, i * m + j + 1, 1);
  }
  cout << max_flow(0, n * m + 1) << endl;
  int xi, yi;
  rep(i, n) rep(j, m) {
    if ((i + j) % 2)
      continue;
    for (auto e : path[i * m + j + 1]) {
      if (e.cap == 0) {
        xi = (e.to - 1) / m;
        yi = (e.to - 1) % m;
        if (xi - i == 1 && yi - j == 0) {
          s[i][j] = 'v';
          s[xi][yi] = '^';
        }
        if (xi - i == -1 && yi - j == 0) {
          s[i][j] = '^';
          s[xi][yi] = 'v';
        }
        if (xi - i == 0 && yi - j == 1) {
          s[i][j] = '>';
          s[xi][yi] = '<';
        }
        if (xi - i == 0 && yi - j == -1) {
          s[i][j] = '<';
          s[xi][yi] = '>';
        }
        break;
      }
    }
  }
  rep(i, n) cout << s[i] << endl;
}