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

#include <algorithm>
#include <array>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <valarray>
#include <vector>

using namespace std;
typedef long long ll;

/**
 * F-F ??????????????¨?????????????????¨
 */
template <int V> struct MaxFlow {
  struct Edge {
    int to, rev, cap;
  };
  vector<Edge> g[V];
  /// ?????????
  void init() {
    for (int i = 0; i < V; i++) {
      g[i].clear();
    }
  }
  /// ??????????????????
  void add(int from, int to, int cap) {
    g[from].push_back(Edge{to, (int)g[to].size(), cap});
    g[to].push_back(Edge{from, (int)g[from].size() - 1, 0});
  }
  /// ??????????????????
  /// ??????????????¨?????????,
  /// ??????1??????2????????????????????????????????????
  void add_multi(int from, int to, int cap) {
    g[from].push_back(Edge{to, (int)g[to].size(), cap});
    g[to].push_back(Edge{from, (int)g[from].size() - 1, cap});
  }

  int erase(int from, int to) {
    for (int i = 0; i < (int)g[from].size(); i++) {
      auto e = g[from][i];
      if (e.to == to && e.cap + g[to][e.rev].cap) {
        int c = g[to][e.rev].cap - e.cap;
        g[to][e.rev].cap = 0;
        g[from][i].cap = 0;
        return c;
      }
    }
    assert(false);
  }

  bool used[V];
  bool dfs(int s, int t) {
    if (s == t)
      return true;
    used[s] = true;
    for (auto &e : g[s]) {
      if (used[e.to])
        continue;
      assert(e.cap >= 0);
      if (!e.cap)
        continue;
      if (dfs(e.to, t)) {
        e.cap -= 1;
        g[e.to][e.rev].cap += 1;
        return true;
      }
    }
    return false;
  }
  int exec(int s, int t, int F) {
    int f = 0;
    while (f < F) {
      fill_n(used, V, false);
      if (!dfs(s, t))
        break;
      f++;
    }
    return f;
  }
  void print() {
    int ec = 0, cs = 0;
    for (int i = 0; i < V; i++) {
      for (auto &e : g[i]) {
        if (e.cap + g[e.to][e.rev].cap == 0)
          continue;
        ec++;
        cs += e.cap;
        printf("%d %d %d\\n", i, e.to, e.cap);
      }
    }
    printf("print %d\\n", ec);
  }
};

MaxFlow<550> mf;

int main() {
  int n, e, q;
  cin >> n >> e >> q;
  for (int i = 0; i < e; i++) {
    int f, t;
    cin >> f >> t;
    f--;
    t--;
    mf.add_multi(f, t, 1);
  }
  int f = 0;
  for (int i = 0; i < q; i++) {
    int m, a, b;
    cin >> m >> a >> b;
    a--;
    b--;
    if (m == 1) {
      mf.add_multi(a, b, 1);
    } else {
      int u = mf.erase(a, b);
      //            mf.print();
      //            printf("erase %d %d %d\\n", a, b, u);
      if (u) {
        if (u == -2)
          swap(a, b);
        if (!mf.exec(a, b, 1)) {
          f--;
          assert(mf.exec(n - 1, b, 1));
          assert(mf.exec(a, 0, 1));
        }
      }
    }
    f += mf.exec(0, n - 1, 100000);
    printf("%d\\n", f);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <array>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <valarray>
#include <vector>

using namespace std;
typedef long long ll;

/**
 * F-F ??????????????¨?????????????????¨
 */
template <int V> struct MaxFlow {
  struct Edge {
    int to, rev, cap;
  };
  vector<Edge> g[V];
  /// ?????????
  void init() {
    for (int i = 0; i < V; i++) {
      g[i].clear();
    }
  }
  /// ??????????????????
  void add(int from, int to, int cap) {
    g[from].push_back(Edge{to, (int)g[to].size(), cap});
    g[to].push_back(Edge{from, (int)g[from].size() - 1, 0});
  }
  /// ??????????????????
  /// ??????????????¨?????????,
  /// ??????1??????2????????????????????????????????????
  void add_multi(int from, int to, int cap) {
    g[from].push_back(Edge{to, (int)g[to].size(), cap});
    g[to].push_back(Edge{from, (int)g[from].size() - 1, cap});
  }

  int erase(int from, int to) {
    for (int i = 0; i < (int)g[from].size(); i++) {
      auto e = g[from][i];
      if (e.to == to && e.cap + g[to][e.rev].cap) {
        int c = g[to][e.rev].cap - e.cap;
        g[to][e.rev].cap = 0;
        g[from][i].cap = 0;
        return c;
      }
    }
    assert(false);
  }

  bool used[V];
  bool dfs(int s, int t) {
    if (s == t)
      return true;
    used[s] = true;
    for (auto &e : g[s]) {
      if (used[e.to])
        continue;
      if (!e.cap)
        continue;
      if (dfs(e.to, t)) {
        e.cap -= 1;
        g[e.to][e.rev].cap += 1;
        return true;
      }
    }
    return false;
  }
  int exec(int s, int t, int F) {
    int f = 0;
    while (f < F) {
      fill_n(used, V, false);
      if (!dfs(s, t))
        break;
      f++;
    }
    return f;
  }
  void print() {
    int ec = 0, cs = 0;
    for (int i = 0; i < V; i++) {
      for (auto &e : g[i]) {
        ec++;
        cs += e.cap;
      }
    }
  }
};

MaxFlow<550> mf;

int main() {
  int n, e, q;
  cin >> n >> e >> q;
  for (int i = 0; i < e; i++) {
    int f, t;
    cin >> f >> t;
    f--;
    t--;
    mf.add_multi(f, t, 1);
  }
  int f = 0;
  for (int i = 0; i < q; i++) {
    int m, a, b;
    cin >> m >> a >> b;
    a--;
    b--;
    if (m == 1) {
      mf.add_multi(a, b, 1);
    } else {
      int u = mf.erase(a, b);
      if (u) {
        if (u == -2)
          swap(a, b);
        f--;
        assert(mf.exec(n - 1, b, 1));
        assert(mf.exec(a, 0, 1));
      }
    }
    f += mf.exec(0, n - 1, 100000);
    printf("%d\\n", f);
  }
  return 0;
}