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

#include <iostream>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
struct edge {
  int to;
  int cap;
  int rev;
};
struct Ford {
  vector<vector<edge>> G;
  int V;
  vector<int> used;
  Ford(int N) {
    V = N;
    used.resize(N, 0);
    G.resize(N);
  }
  void add(int u, int v, int c) {
    edge e1 = {v, c, (int)G[v].size()};
    G[u].push_back(e1);
    edge e2 = {u, 0, (int)G[u].size() - 1};
    G[v].push_back(e2);
  }
  int dfs(int s, int t, int f) {
    if (s == t)
      return f;
    used[s] = 1;
    for (edge &e : G[s]) {
      if (used[e.to] == 0 && e.cap > 0) {
        long long temp = dfs(e.to, t, min(f, e.cap));
        if (temp > 0) {
          e.cap -= temp;
          G[e.to][e.rev].cap += temp;
          return temp;
        }
      }
    }
    return 0;
  }
  int solve(int s, int t) {
    int ans = 0;
    int INF = 1e9;
    while (1) {
      used.assign(V, 0);
      int res = dfs(s, t, INF);
      if (res == 0)
        return ans;
      ans += res;
    }
  }
};
int main() {
  int N, M;
  cin >> N >> M;
  vector<string> S(N);
  for (int i = 0; i < N; i++)
    cin >> S[i];
  int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1};
  Ford F(N * M + 2);
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      if (S[i][j] == '#')
        continue;
      if ((i + j) % 2 == 1) {
        int s = i * M + j;
        F.add(N * M, s, 1);
        for (int k = 0; k < 4; k++) {
          int nx = i + dx[k], ny = j + dy[k];
          if (0 <= nx && nx < N && 0 <= ny && ny < M && S[nx][ny] == '.') {
            int t = nx * M + ny;
            F.add(s, t, 1);
          }
        }
      } else {
        int s = i * M + j, t = N * M + 1;
        F.add(s, t, 1);
      }
    }
  }
  int f = F.solve(N * M, N * M + 1);
  cout << f << endl;
  auto check = [&](int x, int y, int nx, int ny) {
    if (x == nx && y + 1 == ny)
      S[x][y] = '>', S[nx][ny] = '<';
    else if (x == nx && y - 1 == ny)
      S[x][y] = '<', S[nx][ny] = '>';
    else if (x + 1 == nx && y == ny)
      S[x][y] = 'v', S[nx][ny] = '^';
    else if (x - 1 == nx && y == ny)
      S[x][y] = '^', S[nx][ny] = 'v';
  };
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      if (S[i][j] == '#')
        continue;
      if ((i + j) % 2 == 1) {
        for (edge e : F.G[i * M + j]) {
          if (e.to == N * M || e.to == N * M + 1)
            continue;
          if (e.cap == 0) {
            int ny = e.to % M, nx = e.to / M;
            check(i, j, nx, ny);
          }
        }
      }
    }
  }
  for (int i = 0; i < N; i++)
    cout << S[i] << endl;
}

Error: WA

Fixed submission:

#include <iostream>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
struct edge {
  int to;
  int cap;
  int rev;
};
struct Ford {
  vector<vector<edge>> G;
  int V;
  vector<int> used;
  Ford(int N) {
    V = N;
    used.resize(N, 0);
    G.resize(N);
  }
  void add(int u, int v, int c) {
    edge e1 = {v, c, (int)G[v].size()};
    G[u].push_back(e1);
    edge e2 = {u, 0, (int)G[u].size() - 1};
    G[v].push_back(e2);
  }
  int dfs(int s, int t, int f) {
    if (s == t)
      return f;
    used[s] = 1;
    for (edge &e : G[s]) {
      if (used[e.to] == 0 && e.cap > 0) {
        long long temp = dfs(e.to, t, min(f, e.cap));
        if (temp > 0) {
          e.cap -= temp;
          G[e.to][e.rev].cap += temp;
          return temp;
        }
      }
    }
    return 0;
  }
  int solve(int s, int t) {
    int ans = 0;
    int INF = 1e9;
    while (1) {
      used.assign(V, 0);
      int res = dfs(s, t, INF);
      if (res == 0)
        return ans;
      ans += res;
    }
  }
};
int main() {
  int N, M;
  cin >> N >> M;
  vector<string> S(N);
  for (int i = 0; i < N; i++)
    cin >> S[i];
  int dx[4] = {0, -1, 0, 1}, dy[4] = {-1, 0, 1, 0};
  Ford F(N * M + 2);
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      if (S[i][j] == '#')
        continue;
      if ((i + j) % 2 == 1) {
        int s = i * M + j;
        F.add(N * M, s, 1);
        for (int k = 0; k < 4; k++) {
          int nx = i + dx[k], ny = j + dy[k];
          if (0 <= nx && nx < N && 0 <= ny && ny < M && S[nx][ny] == '.') {
            int t = nx * M + ny;
            F.add(s, t, 1);
          }
        }
      } else {
        int s = i * M + j, t = N * M + 1;
        F.add(s, t, 1);
      }
    }
  }
  int f = F.solve(N * M, N * M + 1);
  cout << f << endl;
  vector<string> ans = S;
  auto check = [&](int x, int y, int nx, int ny) {
    if (x == nx && y + 1 == ny)
      ans[x][y] = '>', ans[nx][ny] = '<';
    else if (x == nx && y - 1 == ny)
      ans[x][y] = '<', ans[nx][ny] = '>';
    else if (x + 1 == nx && y == ny)
      ans[x][y] = 'v', ans[nx][ny] = '^';
    else
      ans[x][y] == '^', ans[nx][ny] = 'v';
  };
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      if (S[i][j] == '#')
        continue;
      if ((i + j) % 2 == 1) {
        for (edge e : F.G[i * M + j]) {
          if (e.cap == 0) {
            int ny = e.to % M, nx = e.to / M;
            check(i, j, nx, ny);
          }
        }
      }
    }
  }
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++)
      cout << ans[i][j];
    cout << endl;
  }
}