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

#include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif

// #define int long long
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(c) begin(c), end(c)
const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9) + 7;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

struct FordFulkerson {
  using Flow = int;
  int n;
  int s, t;
  Flow maxflow;
  vector<vector<Flow>> g;
  vector<bool> used;
  FordFulkerson(int n, int s = -1, int t = -1)
      : n(n), g(n, vector<Flow>(n)), used(n), s(s), t(t), maxflow(0){};
  void addArc(int from, int to, Flow cap) { g[from][to] = cap; }
  void addEdge(int a, int b, Flow cap) {
    addArc(a, b, cap);
    addArc(b, a, cap);
  }
  Flow addEdgeCap(int a, int b, Flow cap) {
    if (cap > 0) {
      g[a][b] += cap;
      g[b][a] += cap;
      fill(used.begin(), used.end(), false);
      maxflow += augment(s, t, cap);
    } else {
      Flow rcap = -cap;
      if (g[a][b] - rcap >= 0 && g[b][a] - rcap >= 0) {
        g[a][b] -= rcap;
        g[b][a] -= rcap;
      } else {
        if (g[a][b] > g[b][a])
          swap(a, b);
        fill(used.begin(), used.end(), false);
        Flow f = augment(a, b, a, b, rcap);
        // dump(a + 1, b + 1);
        // dump(f, rcap);
        if (f < rcap) {
          fill(used.begin(), used.end(), false);
          augment(b, a, b, a, f);
          fill(used.begin(), used.end(), false);
          Flow sub = augment(t, s, rcap - f);
          assert(sub == rcap - f);
          maxflow -= sub;
          if (g[a][b] - rcap >= 0 && g[b][a] - rcap >= 0) {
            g[a][b] -= rcap;
            g[b][a] -= rcap;
          } else {
            fill(used.begin(), used.end(), false);
            f = augment(a, b, a, b, rcap);
            // dump(f, rcap);
            assert(f == rcap);
            g[a][b] += rcap;
            g[a][b] -= rcap;
            g[b][a] -= rcap;
            g[b][a] -= rcap;
          }
        } else {
          g[a][b] += rcap;
          g[a][b] -= rcap;
          g[b][a] -= rcap;
          g[b][a] -= rcap;
        }
      }
    }
    return maxflow;
  }
  Flow maximumFlow(int s, int t) {
    Flow total = 0;
    while (true) {
      fill(used.begin(), used.end(), false);
      Flow f = augment(s, t, INF);
      if (f == 0)
        return maxflow += total;
      total += f;
    }
  }
  Flow augment(int v, int t, Flow f) {
    if (v == t)
      return f;
    used[v] = true;
    for (int to = 0; to < n; to++) {
      if (!used[to] && g[v][to] > 0) {
        Flow d = augment(to, t, min(f, g[v][to]));
        if (d > 0) {
          g[v][to] -= d;
          g[to][v] += d;
          return d;
        }
      }
    }
    return 0;
  }
  Flow augment(int v, int t, int a, int b, Flow f) {
    if (v == t)
      return f;
    used[v] = true;
    for (int to = 0; to < n; to++) {
      if (v == a && to == b)
        continue;
      if (!used[to] && g[v][to] > 0) {
        Flow d = augment(to, t, min(f, g[v][to]));
        if (d > 0) {
          g[v][to] -= d;
          g[to][v] += d;
          return d;
        }
      }
    }
    return 0;
  }
};

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, E, Q;
  cin >> N >> E >> Q;
  FordFulkerson ff(N, 0, N - 1);
  rep(i, 0, E) {
    int F, T;
    cin >> F >> T;
    F--, T--;
    ff.addEdge(F, T, 1);
  }
  // dump(ff.g);
  ff.maximumFlow(0, N - 1);
  // dump(ff.g);
  // dump(ff.maxflow);
  rep(i, 0, Q) {
    int M, A, B;
    cin >> M >> A >> B;
    // dump(M, A, B);
    A--, B--;
    if (M == 1) {
      cout << ff.addEdgeCap(A, B, 1) << endl;
    } else {
      cout << ff.addEdgeCap(A, B, -1) << endl;
    }
    // dump(ff.g);
  }
  return 0;
}

Error: -6
fe0f9139-6e68-4d45-b5b5-1c13783ebdc1.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p01430/C++/s560903677.cpp:61: FordFulkerson::Flow FordFulkerson::addEdgeCap(int, int, Flow): Assertion `f == rcap' failed.


Fixed submission:

#include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif

// #define int long long
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(c) begin(c), end(c)
const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9) + 7;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

struct FordFulkerson {
  using Flow = int;
  int n;
  int s, t;
  Flow maxflow;
  vector<vector<Flow>> g;
  vector<bool> used;
  FordFulkerson(int n, int s = -1, int t = -1)
      : n(n), g(n, vector<Flow>(n)), used(n), s(s), t(t), maxflow(0){};
  void addArc(int from, int to, Flow cap) { g[from][to] = cap; }
  void addEdge(int a, int b, Flow cap) {
    addArc(a, b, cap);
    addArc(b, a, cap);
  }
  Flow addEdgeCap(int a, int b, Flow cap) {
    if (cap > 0) {
      g[a][b] += cap;
      g[b][a] += cap;
      fill(used.begin(), used.end(), false);
      maxflow += augment(s, t, cap);
    } else {
      Flow rcap = -cap;
      if (g[a][b] - rcap >= 0 && g[b][a] - rcap >= 0) {
        g[a][b] -= rcap;
        g[b][a] -= rcap;
      } else {
        if (g[a][b] > g[b][a])
          swap(a, b);
        fill(used.begin(), used.end(), false);
        Flow f = augment(a, b, a, b, rcap);
        dump(a, b);
        dump(f, rcap);
        if (f < rcap) {
          fill(used.begin(), used.end(), false);
          augment(b, a, b, a, f);
          fill(used.begin(), used.end(), false);
          Flow sub = augment(t, s, rcap - f);
          assert(sub == rcap - f);
          maxflow -= sub;
          fill(used.begin(), used.end(), false);
          f = augment(a, b, a, b, rcap);
          assert(f == rcap);
          g[a][b] += rcap;
          g[a][b] -= rcap;
          g[b][a] -= rcap;
          g[b][a] -= rcap;
        }
      }
    }

    return maxflow;
  }
  Flow maximumFlow(int s, int t) {
    Flow total = 0;
    while (true) {
      fill(used.begin(), used.end(), false);
      Flow f = augment(s, t, INF);
      if (f == 0)
        return maxflow += total;
      total += f;
    }
  }
  Flow augment(int v, int t, Flow f) {
    if (v == t)
      return f;
    used[v] = true;
    for (int to = 0; to < n; to++) {
      if (!used[to] && g[v][to] > 0) {
        Flow d = augment(to, t, min(f, g[v][to]));
        if (d > 0) {
          g[v][to] -= d;
          g[to][v] += d;
          return d;
        }
      }
    }
    return 0;
  }
  Flow augment(int v, int t, int a, int b, Flow f) {
    if (v == t)
      return f;
    used[v] = true;
    for (int to = 0; to < n; to++) {
      if (v == a && to == b)
        continue;
      if (!used[to] && g[v][to] > 0) {
        Flow d = augment(to, t, min(f, g[v][to]));
        if (d > 0) {
          g[v][to] -= d;
          g[to][v] += d;
          return d;
        }
      }
    }
    return 0;
  }
};

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, E, Q;
  cin >> N >> E >> Q;
  FordFulkerson ff(N, 0, N - 1);
  rep(i, 0, E) {
    int F, T;
    cin >> F >> T;
    F--, T--;
    ff.addEdge(F, T, 1);
  }
  dump(ff.g);
  ff.maximumFlow(0, N - 1);
  dump(ff.g);
  dump(ff.maxflow);
  rep(i, 0, Q) {
    int M, A, B;
    cin >> M >> A >> B;
    dump(M, A, B);
    A--, B--;
    if (M == 1) {
      cout << ff.addEdgeCap(A, B, 1) << endl;
    } else {
      cout << ff.addEdgeCap(A, B, -1) << endl;
    }
    dump(ff.g);
  }
  return 0;
}