Problem p03718 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

const int maxN = 202, maxM = 2e4 + 5, oo = 0x3f3f3f3f;

struct edge {
  int next, v, rsd;
  edge(int a = 0, int b = 0, int c = 0) : next(a), v(b), rsd(c) {}
};

int n;
int head[maxN], totE;
edge E[maxM];

void addedge(int u, int v, int flow) {
  E[++totE] = edge(head[u], v, flow);
  head[u] = totE;
  E[++totE] = edge(head[v], u, 0);
  head[v] = totE;
}

int s, t;
int cur[maxN], d[maxN];

bool bfs() {
  static int inq[maxN], now;
  static int q[maxN], front, rear;
  inq[q[rear = 1] = s] = ++now;
  front = 0;
  while (front != rear) {
    if (++front == maxN)
      front = 0;
    int u = q[front];
    for (int e = head[u], v; e; e = E[e].next) {
      if (inq[v = E[e].v] == now || E[e].rsd <= 0)
        continue;
      d[v] = d[u] + 1;
      inq[v] = now;
      if (++rear == maxN)
        rear = 0;
      q[rear] = v;
    }
  }
  return inq[t] == now;
}

int dfs(int u, int a) {
  if (u == t || a == 0)
    return a;
  int flow = 0, f;
  for (int &e = cur[u], v; e; e = E[e].next)
    if (d[v = E[e].v] == d[u] + 1 && (f = dfs(v, std::min(E[e].rsd, a)))) {
      flow += f;
      E[e].rsd -= f;
      E[e ^ 1].rsd += f;
      a -= f;
      if (!a)
        break;
    }
  return flow;
}

int maxflow() {
  int flow = 0;
  for (; bfs(); flow += dfs(s, oo)) {
    for (int i = 0; i <= n; ++i)
      cur[i] = head[i];
  }
  return flow;
}

int H, W;
char buf[105];

int main() {
  if (fopen("input.txt", "r")) {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
  }

  scanf("%d%d", &H, &W);
  n = H + W;
  totE = 1;
  for (int i = 1; i <= H; ++i) {
    scanf("%s", buf + 1);
    for (int j = 1; j <= W; ++j) {
      if (buf[j] == 'o')
        addedge(i, j + H, 1), addedge(j + H, i, 1);
      else if (buf[j] == 'S')
        s = i, addedge(i, j + H, oo), addedge(j + H, i, oo);
      else if (buf[j] == 'T')
        t = j + H, addedge(i, j + H, oo), addedge(j + H, i, oo);
    }
  }

  int flow = maxflow();
  printf("%d", flow >= oo ? -1 : flow);

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

const int maxN = 202, maxM = 4e4 + 5, oo = 0x3f3f3f3f;

struct edge {
  int next, v, rsd;
  edge(int a = 0, int b = 0, int c = 0) : next(a), v(b), rsd(c) {}
};

int n;
int head[maxN], totE;
edge E[maxM];

void addedge(int u, int v, int flow) {
  E[++totE] = edge(head[u], v, flow);
  head[u] = totE;
  E[++totE] = edge(head[v], u, 0);
  head[v] = totE;
}

int s, t;
int cur[maxN], d[maxN];

bool bfs() {
  static int inq[maxN], now;
  static int q[maxN], front, rear;
  inq[q[rear = 1] = s] = ++now;
  front = 0;
  while (front != rear) {
    if (++front == maxN)
      front = 0;
    int u = q[front];
    for (int e = head[u], v; e; e = E[e].next) {
      if (inq[v = E[e].v] == now || E[e].rsd <= 0)
        continue;
      d[v] = d[u] + 1;
      inq[v] = now;
      if (++rear == maxN)
        rear = 0;
      q[rear] = v;
    }
  }
  return inq[t] == now;
}

int dfs(int u, int a) {
  if (u == t || a == 0)
    return a;
  int flow = 0, f;
  for (int &e = cur[u], v; e; e = E[e].next)
    if (d[v = E[e].v] == d[u] + 1 && (f = dfs(v, std::min(E[e].rsd, a)))) {
      flow += f;
      E[e].rsd -= f;
      E[e ^ 1].rsd += f;
      a -= f;
      if (!a)
        break;
    }
  return flow;
}

int maxflow() {
  int flow = 0;
  for (; bfs(); flow += dfs(s, oo)) {
    for (int i = 0; i <= n; ++i)
      cur[i] = head[i];
  }
  return flow;
}

int H, W;
char buf[105];

int main() {
  if (fopen("input.txt", "r")) {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
  }

  scanf("%d%d", &H, &W);
  n = H + W;
  totE = 1;
  for (int i = 1; i <= H; ++i) {
    scanf("%s", buf + 1);
    for (int j = 1; j <= W; ++j) {
      if (buf[j] == 'o')
        addedge(i, j + H, 1), addedge(j + H, i, 1);
      else if (buf[j] == 'S')
        s = i, addedge(i, j + H, oo), addedge(j + H, i, oo);
      else if (buf[j] == 'T')
        t = j + H, addedge(i, j + H, oo), addedge(j + H, i, oo);
    }
  }

  int flow = maxflow();
  printf("%d", flow >= oo ? -1 : flow);

  return 0;
}