Problem p03718 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define vpii vector<pair<int, int>>
#define gph map<int, vector<int>>
#define mp make_pair
#define FOR(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR1(i, n) for (int i = 1; i <= (int)(n); i++)
#define FORD0(i, n) for (int i = (int)n; i >= 0; i--)
#define FORD1(i, n) for (int i = (int)n; i >= 1; i--)
#define FORS(i, n) for (; i < (int)(n); i++)
#define debug(X)                                                               \
  { cout << #X << " = " << (X) << endl; }
#define printArr(A, n)                                                         \
  {                                                                            \
    cout << #A << " = ";                                                       \
    FOR(i, n) cout << A[i] << ' ';                                             \
    cout << endl;                                                              \
  }
using namespace std;
#define int long long int
int n;
vector<vector<int>> capacity;
vector<vector<int>> adj;

int bfs(int s, int t, vector<int> &parent) {
  fill(parent.begin(), parent.end(), -1);
  parent[s] = -2;
  queue<pair<int, int>> q;
  q.push({s, 1e9});

  while (!q.empty()) {
    int cur = q.front().first;
    int flow = q.front().second;
    q.pop();
    //  debug(cur);

    for (int next : adj[cur]) {
      if (parent[next] == -1 && capacity[cur][next]) {
        parent[next] = cur;
        int new_flow = min(flow, capacity[cur][next]);
        if (next == t)
          return new_flow;
        q.push({next, new_flow});
      }
    }
  }

  return 0;
}

int maxflow(int s, int t) {
  int flow = 0;
  vector<int> parent(n);
  int new_flow;

  while (new_flow = bfs(s, t, parent)) {
    flow += new_flow;
    int cur = t;
    while (cur != s) {
      int prev = parent[cur];
      capacity[prev][cur] -= new_flow;
      capacity[cur][prev] += new_flow;
      cur = prev;
    }
  }

  return flow;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  capacity.assign(250, vector<int>(250, 1));
  adj.resize(120);
  int h, w;
  cin >> h >> w;
  n = 250;
  char in;
  FOR(i, h) {
    FOR(j, w) {
      cin >> in;
      //       debug(in);
      j += 100;
      //        cout << j << " " << i << endl;

      if (in == 'S') {
        adj[i].pb(202);
        adj[j].pb(202);
        adj[i].pb(j);
        adj[j].pb(i);
        adj[202].pb(j);
        adj[202].pb(i);
        capacity[202][j] = 10000;
        capacity[202][i] = 10000;
        capacity[i][202] = 10000;
        capacity[j][202] = 10000;
      } else if (in == 'T') {
        adj[i].pb(201);
        adj[j].pb(201);
        adj[i].pb(j);
        adj[j].pb(i);
        adj[201].pb(j);
        adj[201].pb(i);
        capacity[201][j] = 10000;
        capacity[201][i] = 10000;
        capacity[i][201] = 10000;
        capacity[j][201] = 10000;
      } else if (in == 'o') {
        adj[i].pb(j);
        adj[j].pb(i);
      }
      j -= 100;
    }
  }
  int ans = maxflow(201, 202);
  if (ans >= 10000) {
    cout << -1;
  } else
    cout << ans;
}


Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define vpii vector<pair<int, int>>
#define gph map<int, vector<int>>
#define mp make_pair
#define FOR(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR1(i, n) for (int i = 1; i <= (int)(n); i++)
#define FORD0(i, n) for (int i = (int)n; i >= 0; i--)
#define FORD1(i, n) for (int i = (int)n; i >= 1; i--)
#define FORS(i, n) for (; i < (int)(n); i++)
#define debug(X)                                                               \
  { cout << #X << " = " << (X) << endl; }
#define printArr(A, n)                                                         \
  {                                                                            \
    cout << #A << " = ";                                                       \
    FOR(i, n) cout << A[i] << ' ';                                             \
    cout << endl;                                                              \
  }
using namespace std;
#define int long long int
int n;
vector<vector<int>> capacity;
vector<vector<int>> adj;

int bfs(int s, int t, vector<int> &parent) {
  fill(parent.begin(), parent.end(), -1);
  parent[s] = -2;
  queue<pair<int, int>> q;
  q.push({s, 1e9});

  while (!q.empty()) {
    int cur = q.front().first;
    int flow = q.front().second;
    q.pop();
    //  debug(cur);

    for (int next : adj[cur]) {
      if (parent[next] == -1 && capacity[cur][next]) {
        parent[next] = cur;
        int new_flow = min(flow, capacity[cur][next]);
        if (next == t)
          return new_flow;
        q.push({next, new_flow});
      }
    }
  }

  return 0;
}

int maxflow(int s, int t) {
  int flow = 0;
  vector<int> parent(n);
  int new_flow;

  while (new_flow = bfs(s, t, parent)) {
    flow += new_flow;
    int cur = t;
    while (cur != s) {
      int prev = parent[cur];
      capacity[prev][cur] -= new_flow;
      capacity[cur][prev] += new_flow;
      cur = prev;
    }
  }

  return flow;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  capacity.assign(250, vector<int>(250, 1));
  adj.resize(250);
  int h, w;
  cin >> h >> w;
  n = 250;
  char in;
  FOR(i, h) {
    FOR(j, w) {
      cin >> in;
      //       debug(in);
      j += 100;
      //        cout << j << " " << i << endl;

      if (in == 'S') {
        adj[i].pb(202);
        adj[j].pb(202);
        adj[i].pb(j);
        adj[j].pb(i);
        adj[202].pb(j);
        adj[202].pb(i);
        capacity[202][j] = 10000;
        capacity[202][i] = 10000;
        capacity[i][202] = 10000;
        capacity[j][202] = 10000;
      } else if (in == 'T') {
        adj[i].pb(201);
        adj[j].pb(201);
        adj[i].pb(j);
        adj[j].pb(i);
        adj[201].pb(j);
        adj[201].pb(i);
        capacity[201][j] = 10000;
        capacity[201][i] = 10000;
        capacity[i][201] = 10000;
        capacity[j][201] = 10000;
      } else if (in == 'o') {
        adj[i].pb(j);
        adj[j].pb(i);
      }
      j -= 100;
    }
  }
  int ans = maxflow(201, 202);
  if (ans >= 10000) {
    cout << -1;
  } else
    cout << ans;
}