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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)

using namespace std;
typedef long long ll;

const int INF = 1000000000;
const int MOD = 1000000007;
const double EPS = 1e-8;

int N, E, Q;
int cap[500][500];
bool used[500];
int dfs(int u, int t, int f) {
  if (u == t)
    return f;
  used[u] = true;
  REP(v, N) if (!used[v] && cap[u][v] > 0) {
    int d = dfs(v, t, min(f, cap[u][v]));
    if (d > 0) {
      cap[u][v] -= d;
      cap[v][u] += d;
      return d;
    }
  }
  return 0;
}
int run(int s, int t, int f) {
  memset(used, 0, sizeof used);
  return dfs(s, t, f);
}
int main() {
  while (cin >> N >> E >> Q && N) {
    memset(cap, 0, sizeof cap);
    REP(_, E) {
      int F, T;
      cin >> F >> T;
      F--;
      T--;
      cap[F][T] = 1;
      cap[T][F] = 1;
    }
    int flow = 0;
    REP(_, Q) {
      int M, A, B;
      cin >> M >> A >> B;
      A--;
      B--;
      if (M == 1) {                               // add edge
        assert(cap[A][B] == 0 && cap[B][A] == 0); // assert no edge
        cap[A][B] = cap[B][A] = 1;                // add edge
      } else {                                    // delete edge
        if (cap[A][B] == 2)
          swap(A, B);
        if (cap[B][A] == 2 && run(A, B, 1) == 1) { // remove cycle
          cap[B][A] = cap[A][B] = 1;
        }
        if (cap[B][A] == 2) { // flowing 0->A->B->N-1
          assert(flow > 0);
          assert(run(N - 1, 0, 1) == 1); // N-1 -> 0
          flow--;
        }
        if (cap[B][A] == 2) { // remove cycle
          assert(run(A, B, 1) == 1);
          cap[B][A]--;
          cap[A][B]++;
        }
        assert(cap[A][B] == 1 && cap[B][A] == 1); // assert no flowing
        cap[A][B] = cap[B][A] = 0;                // remove edge
      }
      // update flow
      while (true) {
        int f = run(0, N - 1, 2);
        if (f == 0)
          break;
        flow += f;
      }
      cout << flow << endl;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)

using namespace std;
typedef long long ll;

const int INF = 1000000000;
const int MOD = 1000000007;
const double EPS = 1e-8;

int N, E, Q;
int cap[500][500];
bool used[500];
int dfs(int u, int t, int f) {
  if (u == t)
    return f;
  used[u] = true;
  REP(v, N) if (!used[v] && cap[u][v] > 0) {
    int d = dfs(v, t, min(f, cap[u][v]));
    if (d > 0) {
      cap[u][v] -= d;
      cap[v][u] += d;
      return d;
    }
  }
  return 0;
}
int main() {
  while (cin >> N >> E >> Q && N) {
    memset(cap, 0, sizeof cap);
    REP(_, E) {
      int F, T;
      cin >> F >> T;
      F--;
      T--;
      cap[F][T] = 1;
      cap[T][F] = 1;
    }
    int flow = 0;
    REP(_, Q) {
      int M, A, B;
      cin >> M >> A >> B;
      A--;
      B--;
      if (M == 1) {                               // add edge
        assert(cap[A][B] == 0 && cap[B][A] == 0); // assert no edge
        cap[A][B] = cap[B][A] = 1;                // add edge
      } else {                                    // delete edge
        if (cap[A][B] == 2)
          swap(A, B);
        if (cap[B][A] == 2 && dfs(A, B, 1) == 1) { // remove cycle
          cap[B][A] = cap[A][B] = 1;
        }
        if (cap[B][A] == 2) { // flowing 0->A->B->N-1
          assert(flow > 0);
          memset(used, 0, sizeof used);
          assert(dfs(N - 1, 0, 1) == 1); // N-1 -> 0
          flow--;
        }
        if (cap[B][A] == 2) { // remove cycle
          memset(used, 0, sizeof used);
          assert(dfs(A, B, 1) == 1);
          cap[B][A]--;
          cap[A][B]++;
        }
        assert(cap[A][B] == 1 && cap[B][A] == 1); // assert no flowing
        cap[A][B] = cap[B][A] = 0;                // remove edge
      }
      // update flow
      while (true) {
        memset(used, 0, sizeof used);
        int f = dfs(0, N - 1, 2);
        if (f == 0)
          break;
        flow += f;
      }
      cout << flow << endl;
    }
  }
  return 0;
}