Problem p03718 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define forE(i, x) for (int i = head[x]; i != -1; i = ne[i])
using namespace std;
typedef long long i64;
typedef unsigned long long u64;
typedef unsigned u32;
typedef pair<int, int> pin;
#define mk(a, b) make_pair(a, b)
#define lowbit(x) ((x) & (-(x)))
#define sqr(a) ((a) * (a))
#define clr(a) (memset((a), 0, sizeof(a)))
#define ls ((x) << 1)
#define rs (((x) << 1) | 1)
#define mid (((l) + (r)) >> 1)
#define pb push_back
#define w1 first
#define w2 second
inline void read(int &x) {
  x = 0;
  int f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  x *= f;
}
inline void judge() {
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
}
/*******************************head*******************************/
const int maxn = 505, maxm = 100005;
int head[maxn], t[maxn << 1], ne[maxn << 1], sap[maxn << 1], S, T, d[maxn], num;
int cur[maxn], cnt[maxn], his[maxn], edge[maxn], pre[maxn];
const int inf = 1e6;
inline void addedge(int x, int y, int z) {
  ne[++num] = head[x];
  head[x] = num;
  t[num] = y;
  sap[num] = z;
  ne[++num] = head[y];
  head[y] = num;
  t[num] = x;
  sap[num] = 0;
}
inline int isap() {
  rep(i, 1, T) cur[i] = head[i], d[i] = 0, cnt[i] = 0;
  cnt[0] = T;
  int i = S, aug = inf, ans = 0, minnum, jl;
  while (d[S] < T) {
    bool flag = 0;
    his[i] = aug;
    for (int j = cur[i]; j != -1; j = ne[j])
      if (d[t[j]] + 1 == d[i] && sap[j]) {
        cur[i] = j;
        edge[i] = j;
        pre[t[j]] = i;
        aug = min(aug, sap[j]);
        flag = 1;
        i = t[j];
        if (i == T) {
          ans += aug;
          while (i != S) {
            i = pre[i];
            sap[edge[i]] -= aug;
            sap[edge[i] ^ 1] += aug;
          }
          aug = inf;
        }
        break;
      }
    if (flag)
      continue;
    minnum = T;
    forE(j, i) if (sap[j] && d[t[j]] < minnum) minnum = d[t[j]], jl = j;
    if (--cnt[d[i]] == 0)
      return ans;
    cnt[d[i] = minnum + 1]++;
    cur[i] = jl;
    if (i != S) {
      i = pre[i];
      aug = his[i];
    }
  }
  return ans;
}
char s[maxn][maxn];
int n, m;
inline int tr(int x, int y) { return x * 2 + y; }
int main() {
  read(n);
  read(m);
  rep(i, 1, n) scanf("%s", s[i] + 1);
  S = n + m + 1, T = S + 1;
  rep(i, 1, T) head[i] = -1;
  num = 1;
  rep(i, 1, n) rep(j, 1, m) {
    if (s[i][j] == 'S') {
      addedge(S, i, inf);
      addedge(S, j + n, inf);
    }
    if (s[i][j] == 'T') {
      addedge(i, T, inf);
      addedge(j + n, T, inf);
    }
    if (s[i][j] == 'o') {
      addedge(i, j + n, 1);
      addedge(j + n, i, 1);
    }
  }
  int res = isap();
  if (res >= inf) {
    puts("-1");
  } else {
    printf("%d\n", res);
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define forE(i, x) for (int i = head[x]; i != -1; i = ne[i])
using namespace std;
typedef long long i64;
typedef unsigned long long u64;
typedef unsigned u32;
typedef pair<int, int> pin;
#define mk(a, b) make_pair(a, b)
#define lowbit(x) ((x) & (-(x)))
#define sqr(a) ((a) * (a))
#define clr(a) (memset((a), 0, sizeof(a)))
#define ls ((x) << 1)
#define rs (((x) << 1) | 1)
#define mid (((l) + (r)) >> 1)
#define pb push_back
#define w1 first
#define w2 second
inline void read(int &x) {
  x = 0;
  int f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  x *= f;
}
inline void judge() {
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
}
/*******************************head*******************************/
const int maxn = 505, maxm = 200005;
int head[maxm], t[maxm << 1], ne[maxm << 1], sap[maxm << 1], S, T, d[maxm], num;
int cur[maxm], cnt[maxm], his[maxm], edge[maxm], pre[maxm];
const int inf = 1e6;
inline void addedge(int x, int y, int z) {
  ne[++num] = head[x];
  head[x] = num;
  t[num] = y;
  sap[num] = z;
  ne[++num] = head[y];
  head[y] = num;
  t[num] = x;
  sap[num] = 0;
}
inline int isap() {
  rep(i, 1, T) cur[i] = head[i], d[i] = 0, cnt[i] = 0;
  cnt[0] = T;
  int i = S, aug = inf, ans = 0, minnum, jl;
  while (d[S] < T) {
    bool flag = 0;
    his[i] = aug;
    for (int j = cur[i]; j != -1; j = ne[j])
      if (d[t[j]] + 1 == d[i] && sap[j]) {
        cur[i] = j;
        edge[i] = j;
        pre[t[j]] = i;
        aug = min(aug, sap[j]);
        flag = 1;
        i = t[j];
        if (i == T) {
          ans += aug;
          while (i != S) {
            i = pre[i];
            sap[edge[i]] -= aug;
            sap[edge[i] ^ 1] += aug;
          }
          aug = inf;
        }
        break;
      }
    if (flag)
      continue;
    minnum = T;
    forE(j, i) if (sap[j] && d[t[j]] < minnum) minnum = d[t[j]], jl = j;
    if (--cnt[d[i]] == 0)
      return ans;
    cnt[d[i] = minnum + 1]++;
    cur[i] = jl;
    if (i != S) {
      i = pre[i];
      aug = his[i];
    }
  }
  return ans;
}
char s[maxn][maxn];
int n, m;
inline int tr(int x, int y) { return x * 2 + y; }
int main() {
  read(n);
  read(m);
  rep(i, 1, n) scanf("%s", s[i] + 1);
  S = n + m + 1, T = S + 1;
  rep(i, 1, T) head[i] = -1;
  num = 1;
  rep(i, 1, n) rep(j, 1, m) {
    if (s[i][j] == 'S') {
      addedge(S, i, inf);
      addedge(S, j + n, inf);
    }
    if (s[i][j] == 'T') {
      addedge(i, T, inf);
      addedge(j + n, T, inf);
    }
    if (s[i][j] == 'o') {
      addedge(i, j + n, 1);
      addedge(j + n, i, 1);
    }
  }
  int res = isap();
  if (res >= inf) {
    puts("-1");
  } else {
    printf("%d\n", res);
  }
  return 0;
}