Problem p03718 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

struct edge {
  int to, cap, rev;
};
vector<edge> X[100009];
bool used[100009];
string S[200];
int H, W;

class Max_Flow {
public:
  void add_edge(int u, int v, int w) {
    X[u].push_back(edge{v, w, (int)X[v].size()});
    X[v].push_back(edge{u, 0, (int)X[u].size() - 1});
  }
  int dfs(int s, int t, int f) {
    if (s == t)
      return f;
    used[s] = true;
    for (int i = 0; i < X[s].size(); i++) {
      if (used[X[s][i].to] == true || X[s][i].cap == 0)
        continue;
      int d = min(X[s][i].cap, dfs(X[s][i].to, t, min(f, X[s][i].cap)));
      if (d <= 0)
        continue;
      X[s][i].cap -= d;
      X[X[s][i].to][X[s][i].rev].cap += d;
      return d;
    }
    return 0;
  }
  int max_flow(int p, int q) {
    int sum = 0;
    while (true) {
      for (int i = 0; i <= 220; i++)
        used[i] = false;
      int D = dfs(p, q, (1 << 30));
      if (D == 0)
        break;
      sum += D;
    }
    return sum;
  }
};

int main() {
  cin >> H >> W;
  for (int i = 0; i < H; i++)
    cin >> S[i];
  int sx = 0, gx = 0;
  Max_Flow X;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (S[i][j] == 'S') {
        X.add_edge(i, j + H, 1000000007);
        X.add_edge(j + H, i, 1000000007);
        sx = i;
      }
      if (S[i][j] == 'T') {
        X.add_edge(i, j + H, 1000000007);
        X.add_edge(j + H, i, 1000000007);
        gx = i;
      }
      if (S[i][j] == 'o') {
        X.add_edge(i, j + H, 1);
        X.add_edge(j + H, i, 1);
      }
    }
  }
  int ret = X.max_flow(sx, gx);
  if (ret >= 100000)
    ret = -1;
  cout << ret << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

struct edge {
  int to, cap, rev;
};
vector<edge> X[100009];
bool used[100009];
string S[200];
int H, W;

class Max_Flow {
public:
  void add_edge(int u, int v, int w) {
    X[u].push_back(edge{v, w, (int)X[v].size()});
    X[v].push_back(edge{u, 0, (int)X[u].size() - 1});
  }
  int dfs(int s, int t, int f) {
    if (s == t)
      return f;
    used[s] = true;
    for (int i = 0; i < X[s].size(); i++) {
      if (used[X[s][i].to] == true || X[s][i].cap == 0)
        continue;
      int d = min(X[s][i].cap, dfs(X[s][i].to, t, min(f, X[s][i].cap)));
      if (d <= 0)
        continue;
      X[s][i].cap -= d;
      X[X[s][i].to][X[s][i].rev].cap += d;
      return d;
    }
    return 0;
  }
  int max_flow(int p, int q) {
    int sum = 0;
    while (true) {
      for (int i = 0; i <= 220; i++)
        used[i] = false;
      int D = dfs(p, q, (1 << 30));
      if (D == 0)
        break;
      sum += D;
      if (D >= 205)
        return 1000000007;
    }
    return sum;
  }
};

int main() {
  cin >> H >> W;
  for (int i = 0; i < H; i++)
    cin >> S[i];
  int sx = 0, gx = 0;
  Max_Flow X;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (S[i][j] == 'S') {
        X.add_edge(i, j + H, 1000000007);
        X.add_edge(j + H, i, 1000000007);
        sx = i;
      }
      if (S[i][j] == 'T') {
        X.add_edge(i, j + H, 1000000007);
        X.add_edge(j + H, i, 1000000007);
        gx = i;
      }
      if (S[i][j] == 'o') {
        X.add_edge(i, j + H, 1);
        X.add_edge(j + H, i, 1);
      }
    }
  }
  int ret = X.max_flow(sx, gx);
  if (ret >= 100000)
    ret = -1;
  cout << ret << endl;
  return 0;
}