Problem p03718 (C++)

Buggy submission (status: Runtime Error):
// #define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define IN(a, b, x) (a <= x && x < b)
#define MP make_pair
#define PB push_back
#define MOD 1000000007
#define INF (1LL << 30)
#define LLINF (1LL << 60)
#define PI 3.14159265359
#define EPS 1e-12
#define int ll

int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};

struct edge {
  int to, cap, rev;
};

vector<edge> g[105];
int level[105], iter[105];

void add_edge(int from, int to, int cap) {
  g[from].PB({to, cap, (int)g[to].size()});
  g[to].PB({from, 0, (int)g[from].size() - 1});
}

void bfs(int s) {
  memset(level, -1, sizeof(level));
  queue<int> que;
  level[s] = 0;
  que.push(s);
  while (que.size()) {
    int v = que.front();
    que.pop();
    for (edge &e : g[v]) {
      if (e.cap > 0 && level[e.to] < 0) {
        level[e.to] = level[v] + 1;
        que.push(e.to);
      }
    }
  }
}

int dfs(int v, int t, int f) {
  if (v == t)
    return f;
  for (int &i = iter[v]; i < (int)g[v].size(); ++i) {
    edge &e = g[v][i];
    if (e.cap > 0 && level[v] < level[e.to]) {
      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 (1) {
    bfs(s);
    if (level[t] < 0)
      return flow;
    memset(iter, 0, sizeof(iter));
    int f;
    while ((f = dfs(s, t, INF)) > 0) {
      flow += f;
    }
  }
}

string a[105];
signed main(void) {
  int h, w;
  cin >> h >> w;
  REP(i, h) cin >> a[i];

  REP(i, h) REP(j, w) {
    if (a[i][j] == 'o') {
      add_edge(i, h + j, 1);
      add_edge(h + j, i, 1);
    } else if (a[i][j] == 'S') {
      add_edge(h + w, i, INF);
      add_edge(h + w, h + j, INF);
      add_edge(i, h + j, INF);
      add_edge(h + j, i, INF);
    } else if (a[i][j] == 'T') {
      add_edge(i, h + w + 1, INF);
      add_edge(h + j, h + w + 1, INF);
      add_edge(i, h + j, INF);
      add_edge(h + j, i, INF);
    }
  }
  ll ans = max_flow(h + w, h + w + 1);
  if (ans >= INF)
    cout << -1 << endl;
  else
    cout << ans << endl;

  return 0;
}


Fixed submission:
// #define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define IN(a, b, x) (a <= x && x < b)
#define MP make_pair
#define PB push_back
#define MOD 1000000007
#define INF (1LL << 30)
#define LLINF (1LL << 60)
#define PI 3.14159265359
#define EPS 1e-12
#define int ll

int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};

struct edge {
  int to, cap, rev;
};

vector<edge> g[200010];
int level[200010], iter[200010];

void add_edge(int from, int to, int cap) {
  g[from].PB({to, cap, (int)g[to].size()});
  g[to].PB({from, 0, (int)g[from].size() - 1});
}

void bfs(int s) {
  memset(level, -1, sizeof(level));
  queue<int> que;
  level[s] = 0;
  que.push(s);
  while (que.size()) {
    int v = que.front();
    que.pop();
    for (edge &e : g[v]) {
      if (e.cap > 0 && level[e.to] < 0) {
        level[e.to] = level[v] + 1;
        que.push(e.to);
      }
    }
  }
}

int dfs(int v, int t, int f) {
  if (v == t)
    return f;
  for (int &i = iter[v]; i < (int)g[v].size(); ++i) {
    edge &e = g[v][i];
    if (e.cap > 0 && level[v] < level[e.to]) {
      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 (1) {
    bfs(s);
    if (level[t] < 0)
      return flow;
    memset(iter, 0, sizeof(iter));
    int f;
    while ((f = dfs(s, t, INF)) > 0) {
      flow += f;
    }
  }
}

string a[105];
signed main(void) {
  int h, w;
  cin >> h >> w;
  REP(i, h) cin >> a[i];

  REP(i, h) REP(j, w) {
    if (a[i][j] == 'o') {
      add_edge(i, h + j, 1);
      add_edge(h + j, i, 1);
    } else if (a[i][j] == 'S') {
      add_edge(h + w, i, INF);
      add_edge(h + w, h + j, INF);
      add_edge(i, h + j, INF);
      add_edge(h + j, i, INF);
    } else if (a[i][j] == 'T') {
      add_edge(i, h + w + 1, INF);
      add_edge(h + j, h + w + 1, INF);
      add_edge(i, h + j, INF);
      add_edge(h + j, i, INF);
    }
  }
  ll ans = max_flow(h + w, h + w + 1);
  if (ans >= INF)
    cout << -1 << endl;
  else
    cout << ans << endl;

  return 0;
}