Problem p03718 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f(i, x, n) for (int i = x; i < (int)(n); ++i)

int const N = 102, M = 40016;
int E, Pa[N], VIS[N], VS, SR, TR;
vector<int> G[N];
pair<int, int> Ed[M];

bool path(int v = SR) {
  if (v == TR)
    return true;
  VIS[v] = VS;
  f(i, 0, G[v].size()) {
    pair<int, int> &e = Ed[G[v][i]];
    if (e.second == 0)
      continue;
    if (VIS[e.first] != VS && path(e.first)) {
      Pa[e.first] = G[v][i];
      return true;
    }
  }
  return false;
}

int fix() {
  int an = 2e9, v = TR;
  while (v != SR) {
    int e = Pa[v];
    an = min(an, Ed[e].second);
    v = Ed[e ^ 1].first;
  }
  v = TR;
  while (v != SR) {
    int e = Pa[v];
    Ed[e].second -= an;
    Ed[e ^ 1].second += an;
    v = Ed[e ^ 1].first;
  }
  return an;
}

inline int MF(int a, int b) {
  SR = a, TR = b;
  int an = 0;
  ++VS;
  while (path())
    an += fix(), ++VS;
  return an;
}

inline void aded(int a, int b, int w1 = 1, int w2 = 0) {
  Ed[E] = make_pair(b, w1);
  Ed[E + 1] = make_pair(a, w2);
  G[a].push_back(E++);
  G[b].push_back(E++);
}

inline void CL() {
  E = 0;
  f(i, 0, N) G[i].clear();
}

char g[100][101];

int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  f(i, 0, n) scanf("%s", g[i]);
  pair<int, int> s, t;
  f(i, 0, n) f(j, 0, m) if (g[i][j] != '.') {
    aded(i, j + n, 1);
    aded(j + n, i, 1);
    if (g[i][j] == 'S')
      s = make_pair(i, j);
    else if (g[i][j] == 'T')
      t = make_pair(i, j);
  }
  aded(n + m, s.first, 1000000);
  aded(n + m, s.second + n, 1000000);
  aded(t.first, n + m + 1, 1000000);
  aded(t.second + n, n + m + 1, 1000000);
  int an = MF(n + m, n + m + 1);
  printf("%d\n", an > 100000 ? -1 : an);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f(i, x, n) for (int i = x; i < (int)(n); ++i)

int const N = 202, M = 40016;
int E, Pa[N], VIS[N], VS, SR, TR;
vector<int> G[N];
pair<int, int> Ed[M];

bool path(int v = SR) {
  if (v == TR)
    return true;
  VIS[v] = VS;
  f(i, 0, G[v].size()) {
    pair<int, int> &e = Ed[G[v][i]];
    if (e.second == 0)
      continue;
    if (VIS[e.first] != VS && path(e.first)) {
      Pa[e.first] = G[v][i];
      return true;
    }
  }
  return false;
}

int fix() {
  int an = 2e9, v = TR;
  while (v != SR) {
    int e = Pa[v];
    an = min(an, Ed[e].second);
    v = Ed[e ^ 1].first;
  }
  v = TR;
  while (v != SR) {
    int e = Pa[v];
    Ed[e].second -= an;
    Ed[e ^ 1].second += an;
    v = Ed[e ^ 1].first;
  }
  return an;
}

inline int MF(int a, int b) {
  SR = a, TR = b;
  int an = 0;
  ++VS;
  while (path())
    an += fix(), ++VS;
  return an;
}

inline void aded(int a, int b, int w1 = 1, int w2 = 0) {
  Ed[E] = make_pair(b, w1);
  Ed[E + 1] = make_pair(a, w2);
  G[a].push_back(E++);
  G[b].push_back(E++);
}

inline void CL() {
  E = 0;
  f(i, 0, N) G[i].clear();
}

char g[100][101];

int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  f(i, 0, n) scanf("%s", g[i]);
  pair<int, int> s, t;
  f(i, 0, n) f(j, 0, m) if (g[i][j] != '.') {
    aded(i, j + n, 1);
    aded(j + n, i, 1);
    if (g[i][j] == 'S')
      s = make_pair(i, j);
    else if (g[i][j] == 'T')
      t = make_pair(i, j);
  }
  aded(n + m, s.first, 1000000);
  aded(n + m, s.second + n, 1000000);
  aded(t.first, n + m + 1, 1000000);
  aded(t.second + n, n + m + 1, 1000000);
  int an = MF(n + m, n + m + 1);
  printf("%d\n", an > 100000 ? -1 : an);
}