Problem p03718 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

template <class T> ostream &operator<<(ostream &os, vector<T> V) {
  os << "[ ";
  for (auto v : V)
    os << v << " ";
  return os << "]";
}
template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) {
  return os << "(" << P.first << "," << P.second << ")";
}

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cout << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...) 1
#endif

#define ll long long
#define ld long double
#define vll vector<ll>
#define pll pair<ll, ll>
#define vpll vector<pll>
#define I insert
#define pb push_back
#define F first
#define S second
#define endl "\n"

struct edge {
  ll x, y, cap, flow;
};
struct DinicFlow {
  // *** change inf accordingly *****
  const ll inf = (1e7);
  vector<edge> e;
  vector<ll> cur, d;
  vector<vector<ll>> adj;
  ll n, source, sink;
  DinicFlow() {}
  DinicFlow(ll v) {
    n = v;
    cur = vector<ll>(n + 1);
    d = vector<ll>(n + 1);
    adj = vector<vector<ll>>(n + 1);
  }
  void addEdge(ll from, ll to, ll cap) {
    edge e1 = {from, to, cap, 0};
    edge e2 = {to, from, 0, 0};
    adj[from].push_back(e.size());
    e.push_back(e1);
    adj[to].push_back(e.size());
    e.push_back(e2);
  }
  ll bfs() {
    queue<ll> q;
    for (ll i = 0; i <= n; ++i)
      d[i] = -1;
    q.push(source);
    d[source] = 0;
    while (!q.empty() and d[sink] < 0) {
      ll x = q.front();
      q.pop();
      for (ll i = 0; i < (ll)adj[x].size(); ++i) {
        ll id = adj[x][i], y = e[id].y;
        if (d[y] < 0 and e[id].flow < e[id].cap) {
          q.push(y);
          d[y] = d[x] + 1;
        }
      }
    }
    return d[sink] >= 0;
  }
  ll dfs(ll x, ll flow) {
    if (!flow)
      return 0;
    if (x == sink)
      return flow;
    for (; cur[x] < (ll)adj[x].size(); ++cur[x]) {
      ll id = adj[x][cur[x]], y = e[id].y;
      if (d[y] != d[x] + 1)
        continue;
      ll pushed = dfs(y, min(flow, e[id].cap - e[id].flow));
      if (pushed) {
        e[id].flow += pushed;
        e[id ^ 1].flow -= pushed;
        return pushed;
      }
    }
    return 0;
  }
  ll maxFlow(ll src, ll snk) {
    this->source = src;
    this->sink = snk;
    ll flow = 0;
    while (bfs()) {
      for (ll i = 0; i <= n; ++i)
        cur[i] = 0;
      while (ll pushed = dfs(source, inf)) {
        flow += pushed;
      }
    }
    return flow;
  }
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout << setprecision(25);
  ll h, w, a, b, c, d, i, j;
  cin >> h >> w;
  char ch;
  DinicFlow fl(h + w);
  ll s, t;
  s = t = 0;
  for (i = 0; i < h; i++) {
    for (j = 0; j < w; j++) {
      cin >> ch;
      if (ch != '.') {
        ll cp = 1;
        if (ch != 'o')
          cp = 2e2 + 15;
        fl.addEdge(i, h + j, cp);
        fl.addEdge(h + j, i, cp);
      }
      if (ch == 'S' || ch == 'T') {
        t = s;
        s = i;
      }
    }
  }
  ll f = fl.maxFlow(s, t);
  if (f > 2e2)
    cout << "-1";
  else
    cout << f;
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

template <class T> ostream &operator<<(ostream &os, vector<T> V) {
  os << "[ ";
  for (auto v : V)
    os << v << " ";
  return os << "]";
}
template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) {
  return os << "(" << P.first << "," << P.second << ")";
}

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cout << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...) 1
#endif

#define ll long long
#define ld long double
#define vll vector<ll>
#define pll pair<ll, ll>
#define vpll vector<pll>
#define I insert
#define pb push_back
#define F first
#define S second
#define endl "\n"

struct edge {
  ll x, y, cap, flow;
};
struct DinicFlow {
  // *** change inf accordingly *****
  const ll inf = (1e7);
  vector<edge> e;
  vector<ll> cur, d;
  vector<vector<ll>> adj;
  ll n, source, sink;
  DinicFlow() {}
  DinicFlow(ll v) {
    n = v;
    cur = vector<ll>(n + 1);
    d = vector<ll>(n + 1);
    adj = vector<vector<ll>>(n + 1);
  }
  void addEdge(ll from, ll to, ll cap) {
    edge e1 = {from, to, cap, 0};
    edge e2 = {to, from, 0, 0};
    adj[from].push_back(e.size());
    e.push_back(e1);
    adj[to].push_back(e.size());
    e.push_back(e2);
  }
  ll bfs() {
    queue<ll> q;
    for (ll i = 0; i <= n; ++i)
      d[i] = -1;
    q.push(source);
    d[source] = 0;
    while (!q.empty() and d[sink] < 0) {
      ll x = q.front();
      q.pop();
      for (ll i = 0; i < (ll)adj[x].size(); ++i) {
        ll id = adj[x][i], y = e[id].y;
        if (d[y] < 0 and e[id].flow < e[id].cap) {
          q.push(y);
          d[y] = d[x] + 1;
        }
      }
    }
    return d[sink] >= 0;
  }
  ll dfs(ll x, ll flow) {
    if (!flow)
      return 0;
    if (x == sink)
      return flow;
    for (; cur[x] < (ll)adj[x].size(); ++cur[x]) {
      ll id = adj[x][cur[x]], y = e[id].y;
      if (d[y] != d[x] + 1)
        continue;
      ll pushed = dfs(y, min(flow, e[id].cap - e[id].flow));
      if (pushed) {
        e[id].flow += pushed;
        e[id ^ 1].flow -= pushed;
        return pushed;
      }
    }
    return 0;
  }
  ll maxFlow(ll src, ll snk) {
    this->source = src;
    this->sink = snk;
    ll flow = 0;
    while (bfs()) {
      for (ll i = 0; i <= n; ++i)
        cur[i] = 0;
      while (ll pushed = dfs(source, inf)) {
        flow += pushed;
      }
    }
    return flow;
  }
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout << setprecision(25);
  ll h, w, a, b, c, d, i, j;
  cin >> h >> w;
  char ch;
  DinicFlow fl(h + w);
  ll s, t;
  s = t = 0;
  for (i = 0; i < h; i++) {
    for (j = 0; j < w; j++) {
      cin >> ch;
      if (ch != '.') {
        ll cp = 1;
        if (ch != 'o')
          cp = 2e2 + 15;
        fl.addEdge(i, h + j, cp);
        fl.addEdge(h + j, i, cp);
      }
      if (ch == 'S' || ch == 'T') {
        t = s;
        s = i;
      }
    }
  }
  if (s == t) {
    cout << "-1";
    return 0;
  }
  ll f = fl.maxFlow(s, t);
  if (f > 2e2)
    cout << "-1";
  else
    cout << f;
}