Problem p03718 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
#define ll long long
using namespace std;
const int maxn = 105, inf = 1e8;
#define pb push_back
struct lines {
  int to, flow, cap;
} l[maxn * maxn * 5];
vector<int> g[maxn];
int cur[maxn], d[maxn], t = -1, S, T;
bool v[maxn];

inline void add(int from, int to, int cap) {
  l[++t] = (lines){to, 0, cap}, g[from].pb(t);
  l[++t] = (lines){from, 0, 0}, g[to].pb(t);
}

inline bool BFS() {
  memset(v, 0, sizeof(v)), v[S] = 1, d[S] = 0;
  queue<int> q;
  q.push(S);
  int x;
  lines e;

  while (!q.empty()) {
    x = q.front(), q.pop();
    for (int i = g[x].size() - 1; i >= 0; i--) {
      e = l[g[x][i]];
      if (e.flow < e.cap && !v[e.to])
        v[e.to] = 1, d[e.to] = d[x] + 1, q.push(e.to);
    }
  }

  return v[T];
}

int dfs(int x, int A) {
  if (x == T || !A)
    return A;
  int flow = 0, f, sz = g[x].size();
  for (int &i = cur[x]; i < sz; i++) {
    lines &e = l[g[x][i]];
    if (d[x] + 1 == d[e.to] && (f = dfs(e.to, min(e.cap - e.flow, A)))) {
      A -= f, flow += f;
      e.flow += f, l[g[x][i] ^ 1].flow -= f;
      if (!A)
        break;
    }
  }

  return flow;
}

inline int max_flow() {
  int an = 0;
  while (BFS()) {
    memset(cur, 0, sizeof(cur));
    an += dfs(S, inf);
  }
  return an;
}

int n, m;
char ch;

int main() {
  scanf("%d%d", &n, &m), S = 0, T = n + m + 1;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      ch = getchar();
      while (ch != '.' && ch != 'o' && ch != 'S' && ch != 'T')
        ch = getchar();
      if (ch != '.')
        add(i, j + n, 1), add(j + n, i, 1);
      if (ch == 'S')
        add(S, i, inf), add(S, j + n, inf);
      else if (ch == 'T')
        add(i, T, inf), add(j + n, T, inf);
    }

  int ans = max_flow();
  if (ans >= inf)
    puts("-1");
  else
    printf("%d\n", ans);

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
#define ll long long
using namespace std;
const int maxn = 305, inf = 1e8;
#define pb push_back
struct lines {
  int to, flow, cap;
} l[maxn * maxn * 5];
vector<int> g[maxn];
int cur[maxn], d[maxn], t = -1, S, T;
bool v[maxn];

inline void add(int from, int to, int cap) {
  l[++t] = (lines){to, 0, cap}, g[from].pb(t);
  l[++t] = (lines){from, 0, 0}, g[to].pb(t);
}

inline bool BFS() {
  memset(v, 0, sizeof(v)), v[S] = 1, d[S] = 0;
  queue<int> q;
  q.push(S);
  int x;
  lines e;

  while (!q.empty()) {
    x = q.front(), q.pop();
    for (int i = g[x].size() - 1; i >= 0; i--) {
      e = l[g[x][i]];
      if (e.flow < e.cap && !v[e.to])
        v[e.to] = 1, d[e.to] = d[x] + 1, q.push(e.to);
    }
  }

  return v[T];
}

int dfs(int x, int A) {
  if (x == T || !A)
    return A;
  int flow = 0, f, sz = g[x].size();
  for (int &i = cur[x]; i < sz; i++) {
    lines &e = l[g[x][i]];
    if (d[x] + 1 == d[e.to] && (f = dfs(e.to, min(e.cap - e.flow, A)))) {
      A -= f, flow += f;
      e.flow += f, l[g[x][i] ^ 1].flow -= f;
      if (!A)
        break;
    }
  }

  return flow;
}

inline int max_flow() {
  int an = 0;
  while (BFS()) {
    memset(cur, 0, sizeof(cur));
    an += dfs(S, inf);
  }
  return an;
}

int n, m;
char ch;

int main() {
  scanf("%d%d", &n, &m), S = 0, T = n + m + 1;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      ch = getchar();
      while (ch != '.' && ch != 'o' && ch != 'S' && ch != 'T')
        ch = getchar();
      if (ch != '.')
        add(i, j + n, 1), add(j + n, i, 1);
      if (ch == 'S')
        add(S, i, inf), add(S, j + n, inf);
      else if (ch == 'T')
        add(i, T, inf), add(j + n, T, inf);
    }

  int ans = max_flow();
  if (ans >= inf)
    puts("-1");
  else
    printf("%d\n", ans);

  return 0;
}