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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define index(xs, x) (int)(lower_bound(all(xs), x) - xs.begin())
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919
#define MOD 1000000007
#define MAX_N 500
struct Edge {
  int to, cap, rev;
  Edge(int to, int cap, int rev) : to(to), cap(cap), rev(rev) {}
};

class Dinic {
public:
  vector<Edge> G[MAX_N];
  int level[MAX_N], iter[MAX_N];

  pair<int, int> add_edge(int from, int to, int cap) {
    G[from].pb(Edge(to, cap, (int)G[to].size()));
    G[to].pb(Edge(from, 0, (int)G[from].size() - 1));
    return make_pair(G[from].size() - 1, G[to].size() - 1);
  }

  void bfs(int s) {
    memset(level, -1, sizeof(level));
    queue<int> q;
    level[s] = 0;
    q.push(s);
    while (!q.empty()) {
      int x = q.front();
      q.pop();
      for (int i = 0; i < G[x].size(); i++) {
        Edge &e = G[x][i];
        if (e.cap > 0 && level[e.to] < 0) {
          level[e.to] = level[x] + 1;
          q.push(e.to);
        }
      }
    }
  }

  int dfs(int x, int t, int f) {
    if (x == t)
      return f;
    for (int &i = iter[x]; i < G[x].size(); i++) {
      Edge &e = G[x][i];
      if (e.cap > 0 && level[x] < level[e.to]) {
        int d = dfs(e.to, t, min(f, e.cap));
        if (d > 0) {
          e.cap -= d;
          G[e.to][e.rev].cap += d;
          return d;
        }
      }
    }
    return 0;
  }

  int max_flow(int s, int t, int lim = INF) {
    int flow = 0;
    while (true) {
      bfs(s);
      if (level[t] < 0)
        return flow;
      memset(iter, 0, sizeof(iter));
      while (lim) {
        int f = dfs(s, t, lim);
        if (f <= 0)
          break;
        flow += f;
        lim -= f;
        if (lim == 0)
          return flow;
      }
    }
  }
};

int N, E, Q;
Dinic dinic;
pair<int, int> G[500][500];

int rem(int s, int t) {
  Edge *e = &dinic.G[s][G[s][t]._1], *rev = &dinic.G[t][G[s][t]._2];
  if (rev->cap == 0) {
    e->cap = rev->cap = 0;
    return 0;
  }
  if (dinic.max_flow(s, t, 1) > 0) {
    e->cap = rev->cap = 0;
    return 0;
  }
  dinic.max_flow(N - 1, t, 1);
  dinic.max_flow(s, 0, 1);
  e->cap = rev->cap = 0;
  return -1;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N >> E >> Q;
  rep(i, N) rep(j, N) if (i != j) G[i][j] = dinic.add_edge(i, j, 0);
  rep(i, E) {
    int f, t;
    cin >> f >> t;
    f--, t--;
    dinic.G[f][G[f][t]._1].cap++;
    dinic.G[t][G[t][f]._1].cap++;
  }
  int f = dinic.max_flow(0, N - 1);
  rep(i, Q) {
    int m, a, b;
    cin >> m >> a >> b;
    a--, b--;
    if (m == 1) {
      dinic.G[a][G[a][b]._1].cap++;
      dinic.G[b][G[b][a]._1].cap++;
      f += dinic.max_flow(0, N - 1, 1);
    } else {
      f += rem(a, b);
      f += rem(b, a);
    }
    cout << f << "\\n";
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define index(xs, x) (int)(lower_bound(all(xs), x) - xs.begin())
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919
#define MOD 1000000007
#define MAX_N 500
struct Edge {
  int to, cap, rev;
  Edge(int to, int cap, int rev) : to(to), cap(cap), rev(rev) {}
};

class Dinic {
public:
  vector<Edge> G[MAX_N];
  int level[MAX_N], iter[MAX_N];

  pair<int, int> add_edge(int from, int to, int cap) {
    G[from].pb(Edge(to, cap, (int)G[to].size()));
    G[to].pb(Edge(from, 0, (int)G[from].size() - 1));
    return make_pair(G[from].size() - 1, G[to].size() - 1);
  }

  void bfs(int s) {
    memset(level, -1, sizeof(level));
    queue<int> q;
    level[s] = 0;
    q.push(s);
    while (!q.empty()) {
      int x = q.front();
      q.pop();
      for (int i = 0; i < G[x].size(); i++) {
        Edge &e = G[x][i];
        if (e.cap > 0 && level[e.to] < 0) {
          level[e.to] = level[x] + 1;
          q.push(e.to);
        }
      }
    }
  }

  int dfs(int x, int t, int f) {
    if (x == t)
      return f;
    for (int &i = iter[x]; i < G[x].size(); i++) {
      Edge &e = G[x][i];
      if (e.cap > 0 && level[x] < level[e.to]) {
        int d = dfs(e.to, t, min(f, e.cap));
        if (d > 0) {
          e.cap -= d;
          G[e.to][e.rev].cap += d;
          return d;
        }
      }
    }
    return 0;
  }

  int max_flow(int s, int t, int lim = INF) {
    int flow = 0;
    while (true) {
      bfs(s);
      if (level[t] < 0)
        return flow;
      memset(iter, 0, sizeof(iter));
      while (lim) {
        int f = dfs(s, t, lim);
        if (f <= 0)
          break;
        flow += f;
        if (f >= INF || flow >= INF)
          return INF;
      }
    }
  }
};

int N, E, Q;
Dinic dinic;
pair<int, int> G[500][500];

int rem(int s, int t) {
  Edge *e = &dinic.G[s][G[s][t]._1], *rev = &dinic.G[t][G[s][t]._2];
  if (rev->cap == 0) {
    e->cap = rev->cap = 0;
    return 0;
  }
  if (rev->cap == 0)
    return 0;
  if (dinic.max_flow(s, t, 1) > 0) {
    e->cap = rev->cap = 0;
    return 0;
  }
  dinic.max_flow(N - 1, 0, 1);
  assert(dinic.max_flow(s, t, 1));
  e->cap = rev->cap = 0;
  return -1;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N >> E >> Q;
  rep(i, N) rep(j, N) if (i != j) G[i][j] = dinic.add_edge(i, j, 0);
  rep(i, E) {
    int f, t;
    cin >> f >> t;
    f--, t--;
    dinic.G[f][G[f][t]._1].cap++;
    dinic.G[t][G[t][f]._1].cap++;
  }
  int f = dinic.max_flow(0, N - 1);
  rep(i, Q) {
    int m, a, b;
    cin >> m >> a >> b;
    a--, b--;
    if (m == 1) {
      dinic.G[a][G[a][b]._1].cap++;
      dinic.G[b][G[b][a]._1].cap++;
      f += dinic.max_flow(0, N - 1);
    } else {
      if (dinic.G[b][G[a][b]._2].cap == 0)
        swap(a, b);
      f += rem(b, a);
      f += rem(a, b);
    }
    cout << f << "\\n";
  }
  return 0;
}