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

#include <bits/stdc++.h>

using namespace std;

struct Edge {
  int to, cap, rev;
};

int cap[500][500] = {0};
void addEdge(int from, int to) { cap[from][to] = cap[to][from] = 1; }

#define INF 1000000000

int N;
bool used[500];
int dfs(int s, int t) {
  if (s == t)
    return 1;
  used[s] = true;

  for (int i = 0; i < N; ++i) {
    if (!used[i] && cap[s][i] > 0) {
      int d = dfs(i, t);
      if (d > 0) {
        --cap[s][i];
        ++cap[i][s];
        return 1;
      }
    }
  }
  return 0;
}
int dfs2(int s, int t) {
  if (s == t)
    return 1;
  used[s] = true;

  for (int i = 0; i < N; ++i) {
    if (!used[i] && cap[s][i] > 1) {
      int d = dfs2(i, t);
      if (d > 0) {
        --cap[s][i];
        ++cap[i][s];
        return 1;
      }
    }
  }
  return 0;
}
int flow(int s, int t) {
  int res = 0;
  while (1) {
    memset(used, false, sizeof(used));
    int f = dfs(s, t);
    if (f > 0) {
      res += f;
    } else {
      break;
    }
  }
  return res;
}

/*
bool used2[500][2];
bool dfs2(int s, int t, int from, int to, bool flag) {
        if (s == t) return flag;
        if (s == to && !flag) return false;
        used2[s][(int)flag] = true;

        for (int i = 0; i < N; ++i) {
                if (s == from && i != to) continue;
                bool nf = flag || (s == from && i == to);
                if (!used2[i][(int)nf] && cap[s][i] > 0) {
                        bool f = dfs2(i, t, from, to, nf);
                        if (f) {
                                --cap[s][i];
                                ++cap[i][s];
                                return true;
                        }
                }
        }
        return false;
}
*/

int removeEdge(int a, int b) {
  int res = 0;
  if (cap[a][b] > 1) {
    return removeEdge(b, a);
  } else {
    int c = cap[a][b];
    cap[a][b] = cap[b][a] = 0;
    if (c < 1) {
      memset(used, false, sizeof(used));
      if (!dfs(a, b)) {
        memset(used, false, sizeof(used));
        int x = dfs(N - 1, b);
        memset(used, false, sizeof(used));
        int y = dfs(a, 0);
        if (x + y < 2) {
          cout << x << " " << y << endl;
          exit(1);
        }
        res = -1;
      }
      //			fill(&used2[0][0], &used2[0][0]+500*2, false);
      //			if ( !dfs2(N-1, 0, b, a, false) ) {
      //				cout << "ERROR" << endl;
      //				while (1);
      //			}
    }
  }
  return res;
}

int main() {
  int E, Q;
  cin >> N >> E >> Q;
  for (int i = 0; i < E; ++i) {
    int F, T;
    cin >> F >> T;
    --F, --T;
    addEdge(F, T);
  }
  int f = flow(0, N - 1);
  for (int t = 0; t < Q; ++t) {
    int m, a, b;
    cin >> m >> a >> b;
    --a, --b;
    if (m == 1) {
      addEdge(a, b);
    } else if (m == 2) {
      f += removeEdge(a, b);
    }
    memset(used, false, sizeof(used));
    f += dfs(0, N - 1);
    cout << f << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

struct Edge {
  int to, cap, rev;
};

int cap[500][500] = {0};
void addEdge(int from, int to) { cap[from][to] = cap[to][from] = 1; }

#define INF 1000000000

int N;
bool used[500];
int dfs(int s, int t) {
  if (s == t)
    return 1;
  used[s] = true;

  for (int i = 0; i < N; ++i) {
    if (!used[i] && cap[s][i] > 0) {
      int d = dfs(i, t);
      if (d > 0) {
        --cap[s][i];
        ++cap[i][s];
        return 1;
      }
    }
  }
  return 0;
}
int dfs2(int s, int t) {
  if (s == t)
    return 1;
  used[s] = true;

  for (int i = 0; i < N; ++i) {
    if (!used[i] && cap[s][i] > 1) {
      int d = dfs2(i, t);
      if (d > 0) {
        --cap[s][i];
        ++cap[i][s];
        return 1;
      }
    }
  }
  return 0;
}
int flow(int s, int t) {
  int res = 0;
  while (1) {
    memset(used, false, sizeof(used));
    int f = dfs(s, t);
    if (f > 0) {
      res += f;
    } else {
      break;
    }
  }
  return res;
}

/*
bool used2[500][2];
bool dfs2(int s, int t, int from, int to, bool flag) {
        if (s == t) return flag;
        if (s == to && !flag) return false;
        used2[s][(int)flag] = true;

        for (int i = 0; i < N; ++i) {
                if (s == from && i != to) continue;
                bool nf = flag || (s == from && i == to);
                if (!used2[i][(int)nf] && cap[s][i] > 0) {
                        bool f = dfs2(i, t, from, to, nf);
                        if (f) {
                                --cap[s][i];
                                ++cap[i][s];
                                return true;
                        }
                }
        }
        return false;
}
*/

int removeEdge(int a, int b) {
  int res = 0;
  if (cap[a][b] > 1) {
    return removeEdge(b, a);
  } else {
    int c = cap[a][b];
    cap[a][b] = cap[b][a] = 0;
    if (c < 1) {
      memset(used, false, sizeof(used));
      int x = dfs2(N - 1, b);
      memset(used, false, sizeof(used));
      int y = dfs2(a, 0);
      if (x + y < 2) {
        cout << x << " " << y << endl;
        exit(1);
      }
      //			fill(&used2[0][0], &used2[0][0]+500*2, false);
      //			if ( !dfs2(N-1, 0, b, a, false) ) {
      //				cout << "ERROR" << endl;
      //				while (1);
      //			}
      res = -1;
    }
  }
  return res;
}

int main() {
  int E, Q;
  cin >> N >> E >> Q;
  for (int i = 0; i < E; ++i) {
    int F, T;
    cin >> F >> T;
    --F, --T;
    addEdge(F, T);
  }
  int f = flow(0, N - 1);
  for (int t = 0; t < Q; ++t) {
    int m, a, b;
    cin >> m >> a >> b;
    --a, --b;
    if (m == 1) {
      addEdge(a, b);
    } else if (m == 2) {
      f += removeEdge(a, b);
    }
    memset(used, false, sizeof(used));
    f += dfs(0, N - 1);
    cout << f << endl;
  }
}