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

#include <bits/stdc++.h>
#define vi vector<int>
#define vvi vector<vector<int>>
using ll = long long;
using ld = long double;
#define INF 1e9
#define EPS 0.0000000001
#define rep(i, n) for (int i = 0; i < n; i++)
#define loop(i, s, n) for (int i = s; i < n; i++)
#define all(in) in.begin(), in.end()
template <class T, class S> void cmin(T &a, const S &b) {
  if (a > b)
    a = b;
}
template <class T, class S> void cmax(T &a, const S &b) {
  if (a < b)
    a = b;
}
#define MAX 9999999
using namespace std;
struct Edge {
  int to, cap, rev;
}; // ikisaki youryou gyakuhen
int indexOf[800][800];
class Dinic { // max flow
public:
  int n;
  vector<vector<Edge>> G;  //[MAX];
  vector<int> level, iter; //[MAX];
  vector<bool> used;
  Dinic(int size) {
    n = size;
    G = vector<vector<Edge>>(n);
  }
  void add_edge(int from, int to, int cap) {

    if (indexOf[from][to] != INF) {

      int tot = indexOf[from][to];
      int tof = indexOf[to][from];
      G[from][tot].cap = G[to][tof].cap = 1;

    } else {
      Edge q = {to, cap, int(G[to].size())};
      indexOf[from][to] = (int)G[from].size();
      G[from].push_back(q);
      q = {from, cap, int(G[from].size() - 1)};
      indexOf[to][from] = (int)G[to].size();
      G[to].push_back(q);
    }
  }
  int getmaxflow(int s, int t, int F) { // from s to t,ford_fulkerson
    int flow = 0;
    if (s == t)
      return 0;
    while (1) {
      bfs(s);
      if (level[t] < 0)
        return flow;
      iter = vector<int>(n);
      int f;
      while ((f = dfs(s, t, F)) > 0)
        flow += f;
    }
  }
  void bfs(int s) {
    level = vector<int>(n, -1);
    queue<int> q;
    level[s] = 0;
    q.push(s);
    while (!q.empty()) {
      int v = q.front();
      q.pop();
      for (int i = 0; i < G[v].size(); i++) {
        Edge &e = G[v][i];
        if (e.cap > 0 && level[e.to] < 0) {
          level[e.to] = level[v] + 1;
          q.push(e.to);
        }
      }
    }
  }
  int dfs(int v, int t, int f) {
    if (v == t)
      return f;

    for (int &i = iter[v]; i < G[v].size(); i++) {
      Edge &e = G[v][i];
      if (level[v] >= level[e.to] || e.cap <= 0)
        continue;
      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 backflow(int v, int t, int f) {
    used = vector<bool>(n, false);
    int res = __backflow(v, t, f);
    return res;
  }
  int __backflow(int v, int t, int f) {
    if (v == t)
      return f;
    used[v] = 1;
    for (int i = 0; i < G[v].size(); i++) {
      Edge &e = G[v][i];
      if (used[e.to] || e.cap <= 0)
        continue;
      int d = __backflow(e.to, t, min(f, e.cap));
      if (d > 0) {
        e.cap -= d;
        G[e.to][e.rev].cap += d;
        return d;
      }
    }
    return 0;
  }
};
signed main() {
  int N, E, Q;
  cin >> N >> E >> Q;
  Dinic fl(N);
  rep(i, 800) rep(j, 800) indexOf[i][j] = INF;
  rep(i, E) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    fl.add_edge(a, b, 1);
  }
  int res = fl.getmaxflow(0, N - 1, INF);
  rep(i, Q) {
    int M, a, b;
    cin >> M >> a >> b;
    --a;
    --b;
    if (M == 1) {
      fl.add_edge(a, b, 1);
    } else {

      int tob = indexOf[a][b];
      int toa = indexOf[b][a];
      if (fl.G[a][tob].cap == 1 && fl.G[b][toa].cap == 1) {

        // edge is not used
        //   cout << "false" << endl;

      } else if (fl.G[a][tob].cap == 2) {
        if (fl.backflow(b, a, 1) == 0) {
          fl.backflow(N - 1, a, 1);
          fl.backflow(b, 0, 1);
          --res;
        }
      } else if (fl.G[b][toa].cap == 2) {
        if (fl.backflow(a, b, 1) == 0) {
          fl.backflow(N - 1, b, 1);
          fl.backflow(a, 0, 1);
          --res;
        }
      }

      // erase edge
      fl.G[a][tob].cap = 0;
      fl.G[b][toa].cap = 0;
    }
    res += fl.getmaxflow(0, N - 1, INF);
    cout << res << endl;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define vi vector<int>
#define vvi vector<vector<int>>
using ll = long long;
using ld = long double;
#define INF 1e9
#define EPS 0.0000000001
#define rep(i, n) for (int i = 0; i < n; i++)
#define loop(i, s, n) for (int i = s; i < n; i++)
#define all(in) in.begin(), in.end()
template <class T, class S> void cmin(T &a, const S &b) {
  if (a > b)
    a = b;
}
template <class T, class S> void cmax(T &a, const S &b) {
  if (a < b)
    a = b;
}
#define MAX 9999999
using namespace std;
struct Edge {
  int to, cap, rev;
}; // ikisaki youryou gyakuhen
int indexOf[800][800];
class Dinic { // max flow
public:
  int n;
  vector<vector<Edge>> G;  //[MAX];
  vector<int> level, iter; //[MAX];
  Dinic(int size) {
    n = size;
    G = vector<vector<Edge>>(n);
  }
  void add_edge(int from, int to, int cap) {

    if (indexOf[from][to] != INF) {

      int tot = indexOf[from][to];
      int tof = indexOf[to][from];
      G[from][tot].cap = G[to][tof].cap = 1;

    } else {
      Edge q = {to, cap, int(G[to].size())};
      indexOf[from][to] = (int)G[from].size();
      G[from].push_back(q);
      q = {from, cap, int(G[from].size() - 1)};
      indexOf[to][from] = (int)G[to].size();
      G[to].push_back(q);
    }
  }
  int getmaxflow(int s, int t, int F) { // from s to t,ford_fulkerson
    int flow = 0;
    if (s == t)
      return 0;
    while (1) {
      bfs(s);
      if (level[t] < 0)
        return flow;
      iter = vector<int>(n);
      int f;
      while ((f = dfs(s, t, F)) > 0)
        flow += f;
    }
  }
  void bfs(int s) {
    level = vector<int>(n, -1);
    queue<int> q;
    level[s] = 0;
    q.push(s);
    while (!q.empty()) {
      int v = q.front();
      q.pop();
      for (int i = 0; i < G[v].size(); i++) {
        Edge &e = G[v][i];
        if (e.cap > 0 && level[e.to] < 0) {
          level[e.to] = level[v] + 1;
          q.push(e.to);
        }
      }
    }
  }
  int dfs(int v, int t, int f) {
    if (v == t)
      return f;

    for (int &i = iter[v]; i < G[v].size(); i++) {
      Edge &e = G[v][i];
      if (level[v] >= level[e.to] || e.cap <= 0)
        continue;
      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 backflow(int v, int t, int f) {
    if (v == t)
      return f;
    vector<bool> used(n, false);
    used[v] = 1;
    for (int i = 0; i < G[v].size(); i++) {
      Edge &e = G[v][i];
      if (used[e.to] || e.cap <= 0)
        continue;
      int d = backflow(e.to, t, min(f, e.cap));
      if (d > 0) {
        e.cap -= d;
        G[e.to][e.rev].cap += d;
        return d;
      }
    }
    return 0;
  }
};
signed main() {
  int N, E, Q;
  cin >> N >> E >> Q;
  Dinic fl(N);
  rep(i, 800) rep(j, 800) indexOf[i][j] = INF;
  rep(i, E) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    fl.add_edge(a, b, 1);
  }
  int res = fl.getmaxflow(0, N - 1, INF);
  rep(i, Q) {
    int M, a, b;
    cin >> M >> a >> b;
    --a;
    --b;
    if (M == 1) {
      fl.add_edge(a, b, 1);
    } else {

      int tob = indexOf[a][b];
      int toa = indexOf[b][a];
      if (fl.G[a][tob].cap == 1 && fl.G[b][toa].cap == 1) {

        // edge is not used
        //   cout << "false" << endl;

      } else if (fl.G[a][tob].cap == 2) {
        if (fl.getmaxflow(b, a, 1) == 0) {
          fl.backflow(N - 1, a, 1);
          fl.backflow(b, 0, 1);
          --res;
        }
      } else if (fl.G[b][toa].cap == 2) {
        if (fl.getmaxflow(a, b, 1) == 0) {
          fl.backflow(N - 1, b, 1);
          fl.backflow(a, 0, 1);
          --res;
        }
      }

      // erase edge
      fl.G[a][tob].cap = 0;
      fl.G[b][toa].cap = 0;
    }
    res += fl.getmaxflow(0, N - 1, INF);
    cout << res << endl;
  }
}