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

#include <bits/stdc++.h>
using namespace std;
const int INF = 1 << 28;
struct Edge {
  int v;
};
typedef vector<vector<Edge>> Graph;

const int MAXN = 555;

int N, E, Q;
Graph G;
int vis[MAXN];
int cap[MAXN][MAXN];

void addEdge(int from, int to) {
  G[from].push_back((Edge){to});
  G[to].push_back((Edge){from});
  cap[from][to] = cap[to][from] = 1;
}

void eraseEdge(int from, int to) {
  for (int i = 0; i < G[from].size(); ++i) {
    if (G[from][i].v == to) {
      G[from].erase(G[from].begin() + i);
      break;
    }
  }
  for (int i = 0; i < G[to].size(); ++i) {
    if (G[to][i].v == from) {
      G[to].erase(G[to].begin() + i);
      break;
    }
  }
}

int dfs(int v, int t, int f) {
  vis[v] = true;
  if (v == t)
    return f;
  for (int i = 0; i < G[v].size(); ++i) {
    Edge &e = G[v][i];
    if (vis[e.v])
      continue;
    if (cap[v][e.v] == 0)
      continue;
    int d = dfs(e.v, t, min(f, cap[v][e.v]));
    if (d) {
      cap[v][e.v] -= d;
      cap[e.v][v] += d;
      return d;
    }
  }
  return 0;
}

int oneFlow(int s, int t) {
  memset(vis, 0, sizeof(vis));
  return dfs(s, t, 1);
}

int maxFlow(int s, int t) {
  int f = 0;
  while (1) {
    memset(vis, 0, sizeof(vis));
    int d = dfs(s, t, INF);
    if (d == 0)
      return f;
    f += d;
  }
}

int main() {
  while (cin >> N >> E >> Q) {
    G = Graph(N);
    while (E--) {
      int F, T;
      cin >> F >> T;
      --F;
      --T;
      addEdge(F, T);
    }
    int f = maxFlow(0, N - 1);
    while (Q--) {
      int M, A, B;
      cin >> M >> A >> B;
      --A;
      --B;
      if (M == 1) {
        addEdge(A, B);
        f += oneFlow(0, N - 1);
      } else {
        int s, t;
        if (cap[A][B] == 0) {
          s = A;
          t = B;
        } else if (cap[B][A] == 0) {
          s = B;
          t = A;
        } else {
          s = t = -1;
        }
        eraseEdge(A, B);
        if (s != -1) {
          if (oneFlow(s, t) == 0) {
            --f;
            if (f) {
              oneFlow(N - 1, 0);
              oneFlow(s, t);
            } else {
              for (int i = 0; i < N; ++i) {
                for (int j = 0; j < G[i].size(); ++j) {
                  cap[i][G[i][j].v] = 1;
                }
              }
            }
          }
        }
      }
      cout << f << endl;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int INF = 1 << 28;
struct Edge {
  int v;
};
typedef vector<vector<Edge>> Graph;

const int MAXN = 555;

int N, E, Q;
Graph G;
int vis[MAXN];
int cap[MAXN][MAXN];
int es[MAXN][MAXN];

void addEdge(int from, int to) {
  G[from].push_back((Edge){to});
  G[to].push_back((Edge){from});
  cap[from][to] = cap[to][from] = 1;
  es[from][to] = G[from].size() - 1;
  es[to][from] = G[to].size() - 1;
}

void eraseEdge(int from, int to) {
  G[from].erase(G[from].begin() + es[from][to]);
  G[to].erase(G[to].begin() + es[to][from]);
}

int dfs(int v, int t, int f) {
  vis[v] = true;
  if (v == t)
    return f;
  for (int i = 0; i < G[v].size(); ++i) {
    Edge &e = G[v][i];
    if (vis[e.v])
      continue;
    if (cap[v][e.v] == 0)
      continue;
    int d = dfs(e.v, t, min(f, cap[v][e.v]));
    if (d) {
      cap[v][e.v] -= d;
      cap[e.v][v] += d;
      return d;
    }
  }
  return 0;
}

int oneFlow(int s, int t) {
  memset(vis, 0, sizeof(vis));
  return dfs(s, t, 1);
}

int maxFlow(int s, int t) {
  int f = 0;
  while (1) {
    memset(vis, 0, sizeof(vis));
    int d = dfs(s, t, INF);
    if (d == 0)
      return f;
    f += d;
  }
}

int main() {
  while (cin >> N >> E >> Q) {
    G = Graph(N);
    while (E--) {
      int F, T;
      cin >> F >> T;
      --F;
      --T;
      addEdge(F, T);
    }
    int f = maxFlow(0, N - 1);
    while (Q--) {
      int M, A, B;
      cin >> M >> A >> B;
      --A;
      --B;
      if (M == 1) {
        addEdge(A, B);
        f += oneFlow(0, N - 1);
      } else {
        int s, t;
        if (cap[A][B] == 0) {
          s = A;
          t = B;
        } else if (cap[B][A] == 0) {
          s = B;
          t = A;
        } else {
          s = t = -1;
        }
        eraseEdge(A, B);
        if (s != -1) {
          --f;
          if (oneFlow(s, t) == 0) {
            if (f) {
              oneFlow(N - 1, 0);
              oneFlow(s, t);
            } else {
              for (int i = 0; i < N; ++i) {
                for (int j = 0; j < G[i].size(); ++j) {
                  cap[i][G[i][j].v] = 1;
                }
              }
            }
          } else {
            ++f;
          }
        }
      }
      cout << f << endl;
    }
  }
  return 0;
}