Problem p02378 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <stdio.h>
#include <vector>
typedef struct {
  int to;
  int rev;
  int f;
} edge;
int x, y;
std::vector<edge> vec[250];
bool flag[250];

int dfs(int a) {
  int i;
  if (a == x + y + 1)
    return 1;
  flag[a] = false;
  for (i = 0; i < vec[a].size(); ++i)
    if (flag[vec[a][i].to] && vec[a][i].f) {
      edge *e = &vec[a][i];
      int d;
      d = dfs((*e).to);
      if (d) {
        (*e).f--;
        vec[(*e).to][(*e).rev].f++;
        return 1;
      }
    }
  return 0;
}

int main(void) {
  int i, j, k, e, a, b, c, ans = 0;
  scanf("%d%d%d", &x, &y, &e);
  for (i = 0; i < x + y + 2; ++i)
    flag[i] = true;
  flag[0] = false;
  bool ff[250];
  for (i = 0; i < x + y; ++i)
    ff[i] = true;
  for (i = 0; i < e; ++i) {
    scanf("%d%d", &a, &b);
    b += x;
    edge ta, tb;
    ta.to = b, ta.f = 1, ta.rev = vec[b].size();
    tb.to = a, tb.f = 0, tb.rev = vec[a].size();
    vec[a].push_back(ta);
    vec[b].push_back(tb);
    ta.to = x + y, ta.f = 0, ta.rev = vec[x + y].size();
    tb.to = a, tb.f = 1, tb.rev = vec[a].size();
    if (ff[a]) {
      vec[x + y].push_back(tb);
      vec[a].push_back(ta);
    }
    ta.to = x + y + 1, ta.f = 1, ta.rev = vec[x + y + 1].size();
    tb.to = b, tb.f = 0, tb.rev = vec[b].size();
    if (ff[b]) {
      vec[x + y + 1].push_back(tb);
      vec[b].push_back(ta);
    }
    ff[a] = false, ff[b] = false;
  }
  while (1) {
    for (i = 0; i < x + y + 2; ++i)
      flag[i] = true;
    a = dfs(x + y);
    if (a)
      ans++;
    else
      break;
  }
  // for(i = 0; i < x + y + 2; ++i) for(j = 0; j < vec[i].size(); ++j)
  // printf("%d:%d %d\\n", i, vec[i][j].to, vec[i][j].f);
  printf("%d\\n", ans);
  return 0;
}


Error: -6
munmap_chunk(): invalid pointer


Fixed submission:

#include <algorithm>
#include <stdio.h>
#include <vector>
typedef struct {
  int to;
  int rev;
  int f;
} edge;
int x, y;
std::vector<edge> vec[250];
bool flag[250];

int dfs(int a) {
  int i;
  if (a == x + y + 1)
    return 1;
  flag[a] = false;
  for (i = 0; i < vec[a].size(); ++i)
    if (flag[vec[a][i].to] && vec[a][i].f) {
      edge *e = &vec[a][i];
      int d;
      d = dfs((*e).to);
      if (d) {
        (*e).f--;
        vec[(*e).to][(*e).rev].f++;
        return 1;
      }
    }
  return 0;
}

int main(void) {
  int i, j, k, e, a, b, c, ans = 0;
  scanf("%d%d%d", &x, &y, &e);
  for (i = 0; i < x + y + 2; ++i)
    flag[i] = true;
  flag[0] = false;
  bool ff[x + y];
  for (i = 0; i < x + y; ++i)
    ff[i] = true;
  for (i = 0; i < e; ++i) {
    scanf("%d%d", &a, &b);
    b += x;
    edge ta, tb;
    ta.to = b, ta.f = 1, ta.rev = vec[b].size();
    tb.to = a, tb.f = 0, tb.rev = vec[a].size();
    vec[a].push_back(ta);
    vec[b].push_back(tb);
    ta.to = x + y, ta.f = 0, ta.rev = vec[x + y].size();
    tb.to = a, tb.f = 1, tb.rev = vec[a].size();
    if (ff[a])
      vec[x + y].push_back(tb);
    vec[a].push_back(ta);
    ta.to = x + y + 1, ta.f = 1, ta.rev = vec[x + y + 1].size();
    tb.to = b, tb.f = 0, tb.rev = vec[b].size();
    if (ff[b])
      vec[x + y + 1].push_back(tb);
    vec[b].push_back(ta);
    ff[a] = false, ff[b] = false;
  }
  while (1) {
    for (i = 0; i < x + y + 2; ++i)
      flag[i] = true;
    a = dfs(x + y);
    if (a)
      ans++;
    else
      break;
  }
  // for(i = 0; i < x + y + 2; ++i) for(j = 0; j < vec[i].size(); ++j)
  // printf("%d:%d %d\\n", i, vec[i][j].to, vec[i][j].f);
  printf("%d\\n", ans);
  return 0;
}