Problem p03718 (C++)

Buggy submission (status: Runtime Error):
/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author
 */

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define RREP(i, n) for (int i = 1; i <= (n); ++i)
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define DUMP(x) cout << #x << "=" << (x) << endl;
#define pb push_back
#define eb emplace_back
using namespace std;
typedef long long lint;
typedef pair<int, int> pii;
typedef pair<lint, lint> pll;

const int INF = (int)1e9;
struct edge {
  int to, cap, rev;
};

int used[105];
vector<edge> G[105];

void add_edge(int from, int to, int cap) {
  G[from].pb({to, cap, (int)G[to].size()});
  G[to].pb({from, cap, (int)G[from].size() - 1});
}

int dfs(int v, int t, int f) {
  if (v == t)
    return f;
  used[v] = 1;
  for (auto &e : G[v]) {
    if (!used[e.to] and e.cap > 0) {
      int d = dfs(e.to, t, min(f, e.cap));
      if (d > 0) {
        e.cap -= d;
        G[e.to][e.rev].cap += d;
        return d;
      }
    }
  }
  return 0;
}

int max_flow(int s, int t) {
  int flow = 0;
  while (true) {
    memset(used, 0, sizeof(used));
    int f = dfs(s, t, INF);
    if (f == 0)
      return flow;
    flow += f;
  }
}

class FLotusLeaves {
public:
  void solve(std::istream &in, std::ostream &out) {
    ios_base::sync_with_stdio(false);
    int H, W;
    in >> H >> W;
    RREP(i, H) {
      RREP(j, W) {
        char c;
        in >> c;
        if (c == 'S')
          add_edge(0, i, INF), add_edge(0, H + j, INF);
        else if (c == 'T')
          add_edge(i, H + W + 1, INF), add_edge(H + j, H + W + 1, INF);
        else if (c == 'o')
          add_edge(i, H + j, 1);
      }
    }
    int ans = max_flow(0, H + W + 1);
    if (ans >= INF)
      out << "-1\n";
    else
      out << ans << '\n';
  }
};

int main() {
  FLotusLeaves solver;
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  solver.solve(in, out);
  return 0;
}


Fixed submission:
/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author
 */

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define RREP(i, n) for (int i = 1; i <= (n); ++i)
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define DUMP(x) cout << #x << "=" << (x) << endl;
#define pb push_back
#define eb emplace_back
using namespace std;
typedef long long lint;
typedef pair<int, int> pii;
typedef pair<lint, lint> pll;

const int INF = (int)1e9;
struct edge {
  int to, cap, rev;
};

int used[205];
vector<edge> G[205];

void add_edge(int from, int to, int cap) {
  G[from].pb({to, cap, (int)G[to].size()});
  G[to].pb({from, cap, (int)G[from].size() - 1});
}

int dfs(int v, int t, int f) {
  if (v == t)
    return f;
  used[v] = 1;
  for (auto &e : G[v]) {
    if (!used[e.to] and e.cap > 0) {
      int d = dfs(e.to, t, min(f, e.cap));
      if (d > 0) {
        e.cap -= d;
        G[e.to][e.rev].cap += d;
        return d;
      }
    }
  }
  return 0;
}

int max_flow(int s, int t) {
  int flow = 0;
  while (true) {
    memset(used, 0, sizeof(used));
    int f = dfs(s, t, INF);
    if (f == 0)
      return flow;
    flow += f;
  }
}

class FLotusLeaves {
public:
  void solve(std::istream &in, std::ostream &out) {
    ios_base::sync_with_stdio(false);
    int H, W;
    in >> H >> W;
    RREP(i, H) {
      RREP(j, W) {
        char c;
        in >> c;
        if (c == 'S')
          add_edge(0, i, INF), add_edge(0, H + j, INF);
        else if (c == 'T')
          add_edge(i, H + W + 1, INF), add_edge(H + j, H + W + 1, INF);
        else if (c == 'o')
          add_edge(i, H + j, 1);
      }
    }
    int ans = max_flow(0, H + W + 1);
    if (ans >= INF)
      out << "-1\n";
    else
      out << ans << '\n';
  }
};

int main() {
  FLotusLeaves solver;
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  solver.solve(in, out);
  return 0;
}