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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;
const int MAX_V = 1000;
const int INF = 1 << 25;

int V;
struct edge {
  int to, cap, rev;
};

vector<edge> G[MAX_V];
bool used[MAX_V];

void add_edge(int from, int to, int cap) {
  G[from].push_back(edge{to, cap, int(G[to].size())});
  G[to].push_back(edge{from, 0, int(G[from].size() - 1)});
}

void add_edge2(int from, int to, int cap) {
  G[from].push_back(edge{to, cap, int(G[to].size())});
  G[to].push_back(edge{from, cap, int(G[from].size() - 1)});
}

int dfs(int v, int t, int f) {
  if (v == t)
    return f;
  used[v] = true;
  for (edge &e : G[v]) {
    if (!used[e.to] && e.cap > 0) {
      int d = dfs(e.to, t, min(f, e.cap));
      if (d > 0) {
        e.cap -= d;
        G[e.to][e.rev].cap += d;
        return d;
      }
    }
  }
  return 0;
}

int max_flow(int s, int t, int lim) {
  int flow = 0;
  while (1) {
    fill(used, used + V, false);
    int f = dfs(s, t, lim);
    if (f == 0 || flow == lim)
      return flow;
    flow += f;
    lim -= f;
  }
  return -1;
}

int M[1000];
int A[1000];
int B[1000];

bool ex[1000][1000];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N, E, Q;
  cin >> N >> E >> Q;

  int S = N, T = N + 1;
  V = N + 2;
  for (int i = 0; i < E; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    add_edge2(a, b, 1);
    ex[a][b] = ex[b][a] = true;
  }
  for (int i = 0; i < Q; i++) {
    cin >> M[i] >> A[i] >> B[i];
    A[i]--, B[i]--;
    if (!ex[A[i]][B[i]]) {
      add_edge2(A[i], B[i], 0);
      ex[A[i]][B[i]] = ex[B[i]][A[i]] = true;
    }
  }
  add_edge(S, 0, 1000000);
  add_edge(N - 1, T, 1000000);

  int f = max_flow(S, T, INF);

  for (int i = 0; i < Q; i++) {
    int m = M[i], u = A[i], v = B[i];
    if (m == 1) {
      for (edge &e : G[u]) {
        if (e.to == v) {
          e.cap = G[v][e.rev].cap = 1;
          f += max_flow(S, T, 1);
          break;
        }
      }
    } else {
      for (edge &e : G[u]) {
        if (e.to == v) {
          int c = e.cap;

          if (c == 0) { // u -> v
            if (max_flow(u, v, 1) == 0) {
              max_flow(T, S, 1);
              assert(max_flow(u, v, 1) != 0);
              // assert(max_flow(u, S, 1) == 1);
              // assert(max_flow(T, v, 1) == 1);
              f--;
            }
          } else if (c == 2) { // v -> u
            if (max_flow(v, u, 1) == 0) {
              max_flow(T, S, 1);
              assert(max_flow(v, u, 1) != 0);
              // assert(max_flow(v, S, 1) == 1);
              // assert(max_flow(T, u, 1) == 1);
              f--;
            }
          }
          e.cap = G[v][e.rev].cap = 0;
          break;
        }
      }
    }
    cout << f << endl;
  }
}

Error: -6
3c56699d-d9a4-4b17-a69c-f32186a25573.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p01430/C++/s303309440.cpp:113: int main(): Assertion `max_flow(u, v, 1) != 0' failed.


Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;
const int MAX_V = 1000;
const int INF = 1 << 25;

int V;
struct edge {
  int to, cap, rev;
};

vector<edge> G[MAX_V];
bool used[MAX_V];

void add_edge(int from, int to, int cap) {
  G[from].push_back(edge{to, cap, int(G[to].size())});
  G[to].push_back(edge{from, 0, int(G[from].size() - 1)});
}

void add_edge2(int from, int to, int cap) {
  G[from].push_back(edge{to, cap, int(G[to].size())});
  G[to].push_back(edge{from, cap, int(G[from].size() - 1)});
}

int dfs(int v, int t, int f) {
  if (v == t)
    return f;
  used[v] = true;
  for (edge &e : G[v]) {
    if (!used[e.to] && e.cap > 0) {
      int d = dfs(e.to, t, min(f, e.cap));
      if (d > 0) {
        e.cap -= d;
        G[e.to][e.rev].cap += d;
        return d;
      }
    }
  }
  return 0;
}

int max_flow(int s, int t, int lim) {
  int flow = 0;
  while (1) {
    fill(used, used + V, false);
    int f = dfs(s, t, lim);
    if (f == 0 || flow == lim)
      return flow;
    flow += f;
    lim -= f;
  }
  return -1;
}

int M[1000];
int A[1000];
int B[1000];

bool ex[1000][1000];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N, E, Q;
  cin >> N >> E >> Q;

  int S = N, T = N + 1;
  V = N + 2;
  for (int i = 0; i < E; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    add_edge2(a, b, 1);
    ex[a][b] = ex[b][a] = true;
  }
  for (int i = 0; i < Q; i++) {
    cin >> M[i] >> A[i] >> B[i];
    A[i]--, B[i]--;
    if (!ex[A[i]][B[i]]) {
      add_edge2(A[i], B[i], 0);
      ex[A[i]][B[i]] = ex[B[i]][A[i]] = true;
    }
  }
  add_edge(S, 0, 1000000);
  add_edge(N - 1, T, 1000000);

  int f = max_flow(S, T, INF);

  for (int i = 0; i < Q; i++) {
    int m = M[i], u = A[i], v = B[i];
    if (m == 1) {
      for (edge &e : G[u]) {
        if (e.to == v) {
          e.cap = G[v][e.rev].cap = 1;
          f += max_flow(S, T, 1);
          break;
        }
      }
    } else {
      for (edge &e : G[u]) {
        if (e.to == v) {
          int c = e.cap;
          e.cap = G[v][e.rev].cap = 0;
          if (c == 0) { // u -> v
            if (max_flow(u, v, 1) == 0) {
              max_flow(T, S, 1);
              assert(max_flow(u, v, 1) != 0);
              // assert(max_flow(u, S, 1) == 1);
              // assert(max_flow(T, v, 1) == 1);
              f--;
            }
          } else if (c == 2) { // v -> u
            if (max_flow(v, u, 1) == 0) {
              max_flow(T, S, 1);
              assert(max_flow(v, u, 1) != 0);
              // assert(max_flow(v, S, 1) == 1);
              // assert(max_flow(T, u, 1) == 1);
              f--;
            }
          }
          break;
        }
      }
    }
    cout << f << endl;
  }
}