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

#include <bits/stdc++.h>
#define show(x) cerr << #x << " = " << (x) << endl
using namespace std;
template <typename Functor> struct fix_type {
  Functor functor;
  template <typename... Args>
  decltype(auto) operator()(Args &&...args) const & {
    return functor(functor, std::forward<Args>(args)...);
  }
};
template <typename Functor>
fix_type<typename std::decay<Functor>::type> fix(Functor &&functor) {
  return {std::forward<Functor>(functor)};
}
constexpr int INF = 1 << 30;
int main() {
  int N, E, Q;
  cin >> N >> E >> Q;
  vector<map<int, int>> c(N);
  auto addEdge = [&](const int u, const int v) -> void {
    c[u][v] = c[v][u] = 1;
  };
  auto cutEdge = [&](const int u, const int v) -> void {
    c[u].erase(v), c[v].erase(u);
  };
  auto flow = [&](const int s, const int t, const int limit) {
    vector<bool> used(N, false);
    auto dfs =
        fix([&](auto &&self, const int s, const int t, const int f) -> int {
          if (s == t) {
            return f;
          }
          used[s] = true;
          for (auto &e : c[s]) {
            if (used[e.first] or e.second == 0) {
              continue;
            }
            const int res = min(f, e.second);
            const int d = self(self, e.first, t, res);
            if (d == 0) {
              continue;
            }
            return e.second -= d, c[e.first][s] += d, d;
          }
          return 0;
        });
    for (int flow = 0; flow < limit;) {
      fill(used.begin(), used.end(), false);
      const int f = dfs(s, t, limit - flow);
      if (f == 0) {
        return flow;
      }
      flow += f;
    }
    return limit;
  };
  for (int i = 0, F, T; i < E; i++) {
    cin >> F >> T;
    addEdge(F - 1, T - 1);
  }
  int ans = flow(0, N - 1, INF);
  for (int q = 0; q < Q; q++) {
    int M, A, B;
    cin >> M >> A >> B;
    A--, B--;
    if (M == 1) {
      addEdge(A, B);
      ans += flow(0, N - 1, INF);
    } else {
      const int cap = c[A][B];
      cutEdge(A, B);
      if (cap == 2) {
        swap(A, B);
      }
      if (cap != 1 and flow(A, B, 1) == 0) {
        ans--, flow(A, 0, 1), flow(N - 1, B, 1);
      }
    }
    cout << ans << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define show(x) cerr << #x << " = " << (x) << endl
using namespace std;
template <typename T> constexpr T INF = numeric_limits<T>::max() / 16;
template <typename Functor> struct fix_type {
  Functor functor;
  template <typename... Args>
  decltype(auto) operator()(Args &&...args) const & {
    return functor(functor, std::forward<Args>(args)...);
  }
};
template <typename Functor>
fix_type<typename std::decay<Functor>::type> fix(Functor &&functor) {
  return {std::forward<Functor>(functor)};
}
class Flow {
public:
  using T = int;
  struct Edge {
    int from, to, revind;
    T capacity, flow;
    bool reversed;
  };
  Flow(const int v) : V{v}, edge(v) {}
  int addEdge(const int from, const int to, const T capacity) {
    return edge[from].push_back(
               Edge{from, to, (int)edge[to].size(), capacity, 0, false}),
           edge[to].push_back(Edge{to, from, (int)edge[from].size() - 1,
                                   capacity, capacity, true}),
           edge[from].size() - 1;
  }
  T FordFulkerson(const int s, const int t, const T limit = INF<T>) {
    vector<bool> checked(V);
    auto dfs = fix([&](auto &&self, const int pos, const T &flow) -> T {
      if (pos == t) {
        return flow;
      }
      checked[pos] = true;
      for (auto &e : edge[pos]) {
        if (checked[e.to]) {
          continue;
        }
        const T res = e.capacity - e.flow;
        if (res == 0) {
          continue;
        }
        const T d = self(self, e.to, min(flow, res));
        if (d > 0) {
          return e.flow += min(d, res),
                 edge[e.to][e.revind].flow -= min(d, res), d;
        }
      }
      return 0;
    });
    T flow = 0;
    while (true) {
      for (int i = 0; i < V; i++) {
        checked[i] = false;
      }
      const T f = dfs(s, limit);
      flow += f;
      if (f == 0 or flow >= limit) {
        break;
      }
    }
    return flow;
  }
  const int V;
  vector<vector<Edge>> edge;
};

int main() {
  int N, E, Q;
  cin >> N >> E >> Q;
  Flow f(N);
  map<pair<int, int>, pair<int, int>> mp;
  for (int i = 0; i < E; i++) {
    int F, T;
    cin >> F >> T;
    F--, T--;
    mp[{T, F}] = mp[{F, T}] = {f.addEdge(F, T, 1), f.addEdge(T, F, 1)};
  }
  int flow = f.FordFulkerson(0, N - 1);
  for (int q = 0; q < Q; q++) {
    int M, A, B;
    cin >> M >> A >> B;
    A--, B--;
    if (M == 1) {
      mp[{B, A}] = mp[{A, B}] = {f.addEdge(A, B, 1), f.addEdge(B, A, 1)};
      cout << (flow += f.FordFulkerson(0, N - 1, 1)) << endl;
    } else {
      const auto es = mp[{A, B}];
      const int i1 = es.first, i2 = es.second, r1 = f.edge[A][i1].revind,
                r2 = f.edge[B][i2].revind;
      auto &e1 = f.edge[A][i1], &e2 = f.edge[B][i2], &e3 = f.edge[A][r2],
           &e4 = f.edge[B][r1];
      if (e1.flow + e3.flow == 1) {
        cout << flow << endl;
        e1.capacity = e1.flow = e2.capacity = e2.flow = e3.capacity = e3.flow =
            e4.capacity = e4.flow = 0;
        continue;
      }
      flow--;
      const bool A_B = e1.flow == 1;
      const int u = A_B ? A : B, v = A_B ? B : A;
      e1.capacity = e1.flow = e2.capacity = e2.flow = e3.capacity = e3.flow =
          e4.capacity = e4.flow = 0;
      f.FordFulkerson(u, 0, 1), f.FordFulkerson(N - 1, v, 1);
      cout << (flow += f.FordFulkerson(0, N - 1, 1)) << endl;
    }
  }
  return 0;
}