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

#include <bits/stdc++.h>
using namespace std;

struct Edge {
  int from, to, cap, rev;
};
using Graph = vector<vector<Edge>>;
class Flow {
  Graph g;
  vector<int> used;
  int S, T;
  int flow = 0;
  const int INF = 1e9;
  int bound;
  int dfs(int v, int t, int f) {
    used[v] = true;
    if (v == t)
      return f;
    for (auto &e : g[v]) {
      if (used[e.to] || e.cap <= bound)
        continue;
      int ret = dfs(e.to, t, min(f, e.cap));
      if (ret) {
        e.cap -= ret;
        g[e.to][e.rev].cap += ret;
        return ret;
      }
    }
    return 0;
  }
  int dash(int v, int t, int f) {
    int ret = dfs(v, t, f);
    fill(used.begin(), used.end(), false);
    return ret;
  }

public:
  Flow(int n, int S, int T) : g(n), used(n), S(S), T(T) {}

  void addEdge(int a, int b) {
    g[a].push_back(Edge{a, b, 1, (int)g[b].size()});
    g[b].push_back(Edge{b, a, 1, (int)g[a].size() - 1});
  }

  void removeEdge(int a, int b) {
    auto remove = [&](int from, int to) {
      bound = 1;
      for (auto &e : g[to]) {
        if (e.to == from) {
          if (e.cap == 2) {
            e.cap = 0;
            g[e.to][e.rev].cap = 0;
            if (dash(from, S, 1) == 1) {
              dash(T, to, 1);
              flow--;
            } else {

              dash(from, to, 1);
            }
          } else if (e.cap == 1) {
            e.cap = 0;
            g[e.to][e.rev].cap = 0;
          }
        }
      }
    };
    remove(a, b);
    remove(b, a);
  }

  int maxFlow() {
    bound = 0;
    int f;
    while (f = dash(S, T, INF)) {
      flow += f;
    }
    return flow;
  }
};
int main() {
  int n, e, q;
  cin >> n >> e >> q;
  Flow flow(n, 0, n - 1);
  for (int i = 0; i < e; i++) {
    int f, t;
    cin >> f >> t;
    f--, t--;
    flow.addEdge(f, t);
  }
  while (q--) {
    int m, a, b;
    cin >> m >> a >> b;
    a--, b--;
    if (m == 1) {
      flow.addEdge(a, b);
    } else {
      flow.removeEdge(a, b);
    }
    cout << flow.maxFlow() << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using Weight = int;

struct Edge {
  int from, to;
  Weight cap;
  bool rev;
};

bool operator<(const Edge &lhs, const Edge &rhs) {
  if (lhs.from != rhs.from)
    return lhs.from < rhs.from;
  if (lhs.to != rhs.to)
    return lhs.to < rhs.to;
  if (lhs.cap != rhs.cap)
    return lhs.cap < rhs.cap;
  return lhs.rev < rhs.rev;
}
using Edges = multiset<Edge>;
using Graph = vector<Edges>;

class Flow {
  Graph g;
  Weight nowF = 0;
  const Weight INF = 1e9;
  Weight dfs(int v, int t, stack<int> &st, vector<int> &used, bool mustrev) {
    if (used[v])
      return 0;
    if (v == t)
      return INF;
    used[v] = true;
    for (auto &e : g[v]) {
      if (e.cap == 0)
        continue;
      if (mustrev && !e.rev)
        continue;
      Weight f = dfs(e.to, t, st, used, mustrev);
      if (f > 0) {
        st.push(v);
        return min(f, e.cap);
      }
    }
    return 0;
  }
  Edges::iterator rettf(int fr, int to, int cap, Edges &edges) {
    auto res = edges.find(Edge{fr, to, cap, true});
    if (res == edges.end())
      res = edges.find(Edge{fr, to, cap, false});
    return res;
  }

public:
  Flow(int n) : g(n) {}
  void addEdge(int from, int to, Weight cap) {
    g[from].insert(Edge{from, to, cap, false});
    g[to].insert(Edge{to, from, 0, true});
  }
  Weight maxFlow(int s, int t) {
    Weight res = 0;
    Weight f;
    vector<int> used(g.size(), false);
    stack<int> st;
    while (f = dfs(s, t, st, used, false)) {
      res += f;
      while (!st.empty()) {
        int fr = st.top();
        st.pop();
        int to = (st.empty() ? t : st.top());
        auto it0 = rettf(fr, to, 1, g[fr]);
        auto it1 = g[to].find(Edge{to, fr, 0, !(it0->rev)});
        assert(it0 != g[fr].end() && it1 != g[to].end());
        g[fr].insert(Edge{fr, to, 0, it0->rev});
        g[fr].erase(it0);
        g[to].insert(Edge{to, fr, 1, it1->rev});
        g[to].erase(it1);
      }
      fill(used.begin(), used.end(), false);
    }
    return nowF = nowF + res;
  }

  void decEdge(int fr, int to, int s, int t) {
    if (g[fr].find(Edge{fr, to, 0, false}) == g[fr].end()) {
      g[fr].erase(Edge{fr, to, 1, false});
      g[to].erase(Edge{to, fr, 0, true});
      return;
    }
    g[fr].erase(g[fr].find(Edge{fr, to, 0, false}));
    g[to].erase(g[to].find(Edge{to, fr, 1, true}));

    vector<int> used(g.size(), false);
    stack<int> st;
    int f0 = dfs(fr, s, st, used, true);
    assert(f0 > 0);
    while (!st.empty()) {
      int frr = st.top();
      st.pop();
      int too = (st.empty() ? s : st.top());
      auto it0 = g[frr].find(Edge{frr, too, 1, true});
      auto it1 = g[too].find(Edge{too, frr, 0, false});
      g[frr].insert(Edge{frr, too, 0, true});
      g[frr].erase(it0);
      g[too].insert(Edge{too, frr, 1, false});
      g[too].erase(it1);
    }

    fill(used.begin(), used.end(), 0);
    int f1 = dfs(t, to, st, used, true);
    assert(f1 > 0);
    while (!st.empty()) {
      int frr = st.top();
      st.pop();
      int too = (st.empty() ? to : st.top());
      auto it0 = g[frr].find(Edge{frr, too, 1, true});
      auto it1 = g[too].find(Edge{too, frr, 0, false});
      assert(it0 != g[frr].end());
      assert(it1 != g[too].end());
      g[frr].insert(Edge{frr, too, 0, true});
      g[frr].erase(it0);
      g[too].insert(Edge{too, frr, 1, false});
      g[too].erase(it1);
    }
    nowF--;
  }
};

int main() {
  int n, e, q;
  cin >> n >> e >> q;
  Flow flow(n);
  for (int i = 0; i < e; i++) {
    int f, t;
    cin >> f >> t;
    f--, t--;
    flow.addEdge(f, t, 1);
    flow.addEdge(t, f, 1);
  }
  for (int i = 0; i < q; i++) {
    int m, a, b;
    cin >> m >> a >> b;
    a--, b--;
    if (m == 1) {
      flow.addEdge(a, b, 1);
      flow.addEdge(b, a, 1);
    }
    if (m == 2) {
      flow.decEdge(a, b, 0, n - 1);
      flow.decEdge(b, a, 0, n - 1);
    }
    cout << flow.maxFlow(0, n - 1) << endl;
  }
  return 0;
}