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

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;

#define EACH(i, a) for (auto &i : a)
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define RFOR(i, a, b) for (ll i = (b)-1; i >= (a); i--)
#define REP(i, n) for (ll i = 0; i < (n); i++)
#define RREP(i, n) for (ll i = (n)-1; i >= 0; i--)
#define debug(x) cout << #x << ": " << x << endl
#define pb push_back
#define ALL(a) (a).begin(), (a).end()

const ll linf = 1e18;
const int inf = 1e9;
const double eps = 1e-12;
const double pi = acos(-1);

template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  EACH(x, vec) is >> x;
  return is;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) {
  REP(i, vec.size()) {
    if (i)
      os << " ";
    os << vec[i];
  }
  return os;
}
template <typename T> ostream &operator<<(ostream &os, vector<vector<T>> &vec) {
  REP(i, vec.size()) {
    if (i)
      os << endl;
    os << vec[i];
  }
  return os;
}

struct Edge {
  ll to, cap, rev;
};

vector<vector<Edge>> G;

void add_edge(ll from, ll to, ll cap) {
  G[from].pb({to, cap, (ll)G[to].size()});
  G[to].pb({from, cap, (ll)G[from].size() - 1});
}

ll dfs(ll v, ll t, ll f, vector<bool> &used) {
  if (v == t)
    return f;
  if (used[v])
    return 0;
  used[v] = true;
  EACH(e, G[v]) {
    if (e.cap > 0) {
      ll d = dfs(e.to, t, min(f, e.cap), used);
      if (d > 0) {
        e.cap -= d;
        G[e.to][e.rev].cap += d;
        return d;
      }
    }
  }
  return 0;
}

ll max_flow(ll s, ll t, ll fr = linf) {
  ll res = 0;
  while (fr > 0) {
    vector<bool> used(G.size(), false);
    ll f = dfs(s, t, fr, used);
    if (f == 0)
      break;
    res += f;
    fr -= f;
  }
  assert(fr >= 0);
  return res;
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  ll N, E, Q;
  cin >> N >> E >> Q;
  G.resize(N);
  REP(i, E) {
    ll F, T;
    cin >> F >> T;
    --F, --T;
    add_edge(F, T, 1);
  }
  ll s = 0, t = N - 1;
  ll ans = max_flow(s, t);
  REP(i, Q) {
    ll M, A, B;
    cin >> M >> A >> B;
    --A, --B;
    // cout << A << " " << B << " " << M << endl;
    if (M == 1) {
      add_edge(A, B, 1);
    } else {
      ll idx = -1;
      REP(j, G[A].size()) {
        Edge &e = G[A][j];
        if (e.to == B) {
          assert(idx < 0);
          idx = j;
        }
      }
      Edge &e = G[A][idx];
      ll r = e.cap == 0 ? 1 : e.cap == 2 ? -1 : 0;
      // cout << A << " " << B << " " << e.rev << " " << e.to << " " <<
      // G[B][e.rev].to << endl;
      assert(G[B][e.rev].to == A);
      assert(e.cap + G[B][e.rev].cap == 2);
      e.cap = 0, e.to = -1;
      G[B][e.rev].cap = 0, G[B][e.rev].to = -1;
      // G[B].erase(G[B].begin()+e.rev, G[B].begin()+e.rev+1);
      // G[A].erase(G[A].begin()+idx, G[A].begin()+idx+1);
      if (r) {
        if (r == 1) {
          int f1 = max_flow(t, B, 1);
          int f2 = max_flow(A, s, 1);
          if (f1 == 0 || f2 == 0) {
            if (f1 == 1)
              max_flow(B, t, 1);
            if (f2 == 1)
              max_flow(s, A, 1);
            assert(max_flow(A, B, 1) == 1);
          } else {
            --ans;
          }
        } else if (r == -1) {
          int f1 = max_flow(t, A, 1);
          int f2 = max_flow(B, s, 1);
          if (f1 == 0 || f2 == 0) {
            if (f1 == 1)
              max_flow(A, t, 1);
            if (f2 == 1)
              max_flow(s, B, 1);
            assert(max_flow(B, A, 1) == 1);
          } else {
            --ans;
          }
        }
      }
    }
    ans += max_flow(s, t);
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;

#define EACH(i, a) for (auto &i : a)
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define RFOR(i, a, b) for (ll i = (b)-1; i >= (a); i--)
#define REP(i, n) for (ll i = 0; i < (n); i++)
#define RREP(i, n) for (ll i = (n)-1; i >= 0; i--)
#define debug(x) cout << #x << ": " << x << endl
#define pb push_back
#define ALL(a) (a).begin(), (a).end()

const ll linf = 1e18;
const int inf = 1e9;
const double eps = 1e-12;
const double pi = acos(-1);

template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  EACH(x, vec) is >> x;
  return is;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) {
  REP(i, vec.size()) {
    if (i)
      os << " ";
    os << vec[i];
  }
  return os;
}
template <typename T> ostream &operator<<(ostream &os, vector<vector<T>> &vec) {
  REP(i, vec.size()) {
    if (i)
      os << endl;
    os << vec[i];
  }
  return os;
}

struct Edge {
  ll to, cap, rev;
};

vector<vector<Edge>> G;

void add_edge(ll from, ll to, ll cap) {
  G[from].pb({to, cap, (ll)G[to].size()});
  G[to].pb({from, cap, (ll)G[from].size() - 1});
}

ll dfs(ll v, ll t, ll f, vector<bool> &used) {
  if (v == t)
    return f;
  if (used[v])
    return 0;
  used[v] = true;
  EACH(e, G[v]) {
    if (e.cap > 0) {
      ll d = dfs(e.to, t, min(f, e.cap), used);
      if (d > 0) {
        e.cap -= d;
        G[e.to][e.rev].cap += d;
        return d;
      }
    }
  }
  return 0;
}

ll max_flow(ll s, ll t, ll fr = linf) {
  ll res = 0;
  while (fr > 0) {
    vector<bool> used(G.size(), false);
    ll f = dfs(s, t, fr, used);
    if (f == 0)
      break;
    res += f;
    fr -= f;
  }
  return res;
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  ll N, E, Q;
  cin >> N >> E >> Q;
  G.resize(N);
  REP(i, E) {
    ll F, T;
    cin >> F >> T;
    --F, --T;
    add_edge(F, T, 1);
  }
  ll s = 0, t = N - 1;
  ll ans = max_flow(s, t);
  REP(i, Q) {
    ll M, A, B;
    cin >> M >> A >> B;
    --A, --B;
    if (M == 1) {
      add_edge(A, B, 1);
    } else {
      ll idx = -1;
      REP(j, G[A].size()) {
        Edge &e = G[A][j];
        if (e.to == B) {
          idx = j;
          break;
        }
      }
      Edge &e = G[A][idx];
      ll r = e.cap == 0 ? 1 : e.cap == 2 ? -1 : 0;
      // cout << A << " " << B << " " << e.rev << " " << e.to << " " <<
      // G[B][e.rev].to << endl;
      assert(G[B][e.rev].to == A);
      assert(e.cap + G[B][e.rev].cap == 2);
      e.cap = 0, e.to = -1;
      G[B][e.rev].cap = 0, G[B][e.rev].to = -1;
      // G[B].erase(G[B].begin()+e.rev, G[B].begin()+e.rev+1);
      // G[A].erase(G[A].begin()+idx, G[A].begin()+idx+1);
      if (r) {
        if (r == 1) {
          assert(max_flow(t, B, 1) == 1);
          assert(max_flow(A, s, 1) == 1);
        } else if (r == -1) {
          assert(max_flow(t, A, 1) == 1);
          assert(max_flow(B, s, 1) == 1);
        }
        --ans;
      }
    }
    ans += max_flow(s, t);
    cout << ans << endl;
  }
}