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

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

#define int long long
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<pint> vpint;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, f, n) for (int i = (f); i < (n); i++)
#define all(v) (v).begin(), (v).end()
#define each(it, v)                                                            \\
  for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
#define pb push_back
#define fi first
#define se second
template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}

const int INF = 1001001001;

struct edge {
  int to, cap, rev;
  edge(int a, int b, int c) : to(a), cap(b), rev(c) {}
};

int N, E, Q;

const int MAX_V = 600;
vector<edge> G[MAX_V];
bool used[MAX_V];

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

int dfs(int v, int t, int f) {
  if (v == t)
    return f;
  used[v] = true;
  for (int i = 0; i < G[v].size(); i++) {
    edge &e = G[v][i];
    if (used[e.to] || e.cap == 0)
      continue;
    int d = dfs(e.to, t, min(f, e.cap));
    if (d == 0)
      continue;
    e.cap -= d;
    G[e.to][e.rev].cap += d;
    return d;
  }
  return 0;
}

int max_flow(int s, int t, int lim) {
  int f = 0;
  while (lim) {
    memset(used, 0, sizeof(used));
    int ff = dfs(s, t, lim);
    if (ff == 0)
      return f;
    f += ff;
    lim -= ff;
  }
  return f;
}

int f;
void q1(int a, int b) {
  add_edge(a, b, 1);
  f += max_flow(0, N - 1, INF);
}

void q2(int a, int b) {
  bool f1 = false, f2 = false;

  for (auto &e : G[a]) {
    if (e.to != b || e.cap == 0)
      continue;
    if (e.cap == 2)
      f2 = true;
    e.cap = 0;
  }
  for (auto &e : G[b]) {
    if (e.to != a || e.cap == 0)
      continue;
    if (e.cap == 2)
      f1 = true;
    e.cap = 0;
  }
  if (!f1 && !f2)
    return;

  if (f2)
    swap(a, b);

  int tmp = max_flow(a, b, 1);

  if (tmp)
    return;

  add_edge(0, N - 1, 1);
  tmp = max_flow(a, b, 1);
  assert(tmp);
  G[0].pop_back();
  G[N - 1].pop_back();
  f--;

  f += max_flow(0, N - 1, INF);
}

signed main() {

  cin >> N >> E >> Q;
  rep(i, E) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    add_edge(a, b, 1);
  }

  f = max_flow(0, N - 1, INF);

  while (Q--) {
    int t, a, b;
    cin >> t >> a >> b;
    a--;
    b--;
    if (t == 1) {
      q1(a, b);
    } else {
      q2(a, b);
    }
    /*
    cout<<f<<"-------------"<<endl;
    for(int i=0;i<N;i++){
        for(auto &e:G[i]){
            cout<<i+1<<" "<<e.to+1<<" "<<e.cap<<endl;
        }
    }*/
    printf("%lld\\n", f);
  }
  return 0;
}

Error: 0

Fixed submission:

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

#define int long long
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<pint> vpint;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, f, n) for (int i = (f); i < (n); i++)
#define all(v) (v).begin(), (v).end()
#define each(it, v)                                                            \\
  for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
#define pb push_back
#define fi first
#define se second
template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}

const int INF = 1001001001;

struct edge {
  int to, cap, rev;
  edge(int a, int b, int c) : to(a), cap(b), rev(c) {}
};

int N, E, Q;

const int MAX_V = 600;
vector<edge> G[MAX_V];
bool used[MAX_V];

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

int dfs(int v, int t, int f) {
  if (v == t)
    return f;
  used[v] = true;
  for (int i = 0; i < G[v].size(); i++) {
    edge &e = G[v][i];
    if (used[e.to] || e.cap == 0)
      continue;
    int d = dfs(e.to, t, min(f, e.cap));
    if (d == 0)
      continue;
    e.cap -= d;
    G[e.to][e.rev].cap += d;
    return d;
  }
  return 0;
}

int max_flow(int s, int t, int lim) {
  int f = 0;
  while (lim) {
    memset(used, 0, sizeof(used));
    int ff = dfs(s, t, lim);
    if (ff == 0)
      return f;
    f += ff;
    lim -= ff;
  }
  return f;
}

int f;
void q1(int a, int b) {
  add_edge(a, b, 1);
  f += max_flow(0, N - 1, INF);
}

void q2(int a, int b) {
  bool f1 = false, f2 = false;

  for (auto &e : G[a]) {
    if (e.to != b || e.cap != 2)
      continue;
    f2 = true;
    e.cap = 0;
  }
  for (auto &e : G[b]) {
    if (e.to != a || e.cap != 2)
      continue;
    f1 = true;
    e.cap = 0;
  }
  if (!f1 && !f2)
    assert(0);

  if (f2)
    swap(a, b);

  int tmp = max_flow(a, b, 1);

  if (tmp)
    return;

  add_edge(0, N - 1, 1);
  tmp = max_flow(a, b, 1);
  assert(tmp);
  G[0].pop_back();
  G[N - 1].pop_back();
  f--;

  f += max_flow(0, N - 1, INF);
}

signed main() {

  cin >> N >> E >> Q;
  rep(i, E) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    add_edge(a, b, 1);
  }

  f = max_flow(0, N - 1, INF);

  while (Q--) {
    int t, a, b;
    cin >> t >> a >> b;
    a--;
    b--;
    if (t == 1) {
      q1(a, b);
    } else {
      q2(a, b);
    }
    /*
cout<<f<<"-------------"<<endl;
for(int i=0;i<N;i++){
    for(auto &e:G[i]){
        if(e.cap==2)cout<<e.to+1<<" "<<i+1<<endl;
    }
}*/
    printf("%lld\\n", f);
  }
  return 0;
}