Problem p03718 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstring>
#include <vector>

// FILE *fin = fopen("a.in", "r");
#define fin stdin

#define MAXN 109
#define MAXT 11000

char m[MAXN + 2][MAXN + 10];
std::vector<int> g[MAXT];
bool viz[MAXT];
int lin[MAXN], col[MAXN];

bool dfs(int x) {
  if (x == 1)
    return 1;
  if (viz[x])
    return 0;
  viz[x] = 1;
  int p = 0;
  while (p < (int)g[x].size() && !dfs(g[x][p]))
    p++;
  if (p == (int)g[x].size())
    return 0;
  g[g[x][p]].push_back(x);
  std::swap(g[x][p], g[x].back());
  g[x].pop_back();
  return 1;
}

int main() {
  int nrlin, nrcol;
  fscanf(fin, "%d%d ", &nrlin, &nrcol);

  int linS = 0, colS = 0, linD = 0, colD = 0;
  for (int i = 1; i <= nrlin; i++) {
    fgets(m[i] + 1, MAXN + 5, fin);

    for (int j = 1; j <= nrcol; j++)
      if (m[i][j] == 'S')
        linS = i, colS = j;
      else if (m[i][j] == 'T')
        linD = i, colD = j;
  }

  if (linS == linD || colS == colD)
    printf("-1\n");
  else {
    lin[linS] = 0;
    col[colS] = 0;
    lin[linD] = 1;
    col[colD] = 1;
    int t = 1;
    for (int i = 1; i <= nrlin; i++)
      if (i != linS && i != linD)
        lin[i] = ++t;
    for (int i = 1; i <= nrcol; i++)
      if (i != colS && i != colD)
        col[i] = ++t;
    for (int i = 1; i <= nrlin; i++) {
      for (int j = 1; j <= nrcol; j++) {
        if (m[i][j] == 'o') {
          t++;
          g[lin[i]].push_back(t);
          g[col[j]].push_back(t);
          g[t].push_back(t + 1);
          t++;
          g[t].push_back(lin[i]);
          g[t].push_back(col[j]);
        }
      }
    }
    int ans = 0;
    while (dfs(0)) {
      memset(viz, 0, sizeof viz);
      ans++;
    }
    printf("%d\n", ans);
  }

  return 0;
}


Fixed submission:
#include <cstdio>
#include <cstring>
#include <vector>

// FILE *fin = fopen("a.in", "r");
#define fin stdin

#define MAXN 109
#define MAXT 21000

char m[MAXN + 2][MAXN + 10];
std::vector<int> g[MAXT];
bool viz[MAXT];
int lin[MAXN], col[MAXN];

bool dfs(int x) {
  if (x == 1)
    return 1;
  if (viz[x])
    return 0;
  viz[x] = 1;
  int p = 0;
  while (p < (int)g[x].size() && !dfs(g[x][p]))
    p++;
  if (p == (int)g[x].size())
    return 0;
  g[g[x][p]].push_back(x);
  std::swap(g[x][p], g[x].back());
  g[x].pop_back();
  return 1;
}

int main() {
  int nrlin, nrcol;
  fscanf(fin, "%d%d ", &nrlin, &nrcol);

  int linS = 0, colS = 0, linD = 0, colD = 0;
  for (int i = 1; i <= nrlin; i++) {
    fgets(m[i] + 1, MAXN + 5, fin);

    for (int j = 1; j <= nrcol; j++)
      if (m[i][j] == 'S')
        linS = i, colS = j;
      else if (m[i][j] == 'T')
        linD = i, colD = j;
  }

  if (linS == linD || colS == colD)
    printf("-1\n");
  else {
    lin[linS] = 0;
    col[colS] = 0;
    lin[linD] = 1;
    col[colD] = 1;
    int t = 1;
    for (int i = 1; i <= nrlin; i++)
      if (i != linS && i != linD)
        lin[i] = ++t;
    for (int i = 1; i <= nrcol; i++)
      if (i != colS && i != colD)
        col[i] = ++t;
    for (int i = 1; i <= nrlin; i++) {
      for (int j = 1; j <= nrcol; j++) {
        if (m[i][j] == 'o') {
          t++;
          g[lin[i]].push_back(t);
          g[col[j]].push_back(t);
          g[t].push_back(t + 1);
          t++;
          g[t].push_back(lin[i]);
          g[t].push_back(col[j]);
        }
      }
    }
    int ans = 0;
    while (dfs(0)) {
      memset(viz, 0, sizeof viz);
      ans++;
    }
    printf("%d\n", ans);
  }

  return 0;
}