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

#include <bits/stdc++.h>
#define rep(i, a) for (int i = 0; i < (int)(a); i++)
#define pb push_back
#define sz size()
using namespace std;
typedef vector<int> vi;

const int INF = 1e9;

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

struct FordFulkerson {
  int n;
  vector<vector<edge>> graph;
  vi use;

  FordFulkerson(int a = 0) : n(a) {
    graph.resize(a);
    use.resize(a);
  }

  void AddEdge(int s, int g, int c, int p) {
    graph[s].pb(edge(s, g, c, p, graph[g].sz));
    graph[g].pb(edge(g, s, -c, 0, graph[s].sz - 1));
  }

  void DeleteEdge(int s, int g) {
    int id = -1, rev = -1;
    rep(e, graph[s].sz) {
      if (graph[s][e].to == g && graph[s][e].cost > 0) {
        id = e;
        rev = graph[s][e].rev;
      }
    }
    if (id < 0)
      return;
    // cout << id << " " << rev << endl;

    swap(graph[s][id], graph[s].back());
    swap(graph[g][rev], graph[g].back());

    edge e = graph[s][id];
    // cout << s << " " << e.to << " " << e.rev << endl;
    if (id + 1 != (int)graph[s].size())
      graph[e.to][e.rev].rev = id;
    e = graph[g][rev];
    // cout << g << " " << e.to << " " << e.rev << endl;
    if (rev + 1 != (int)graph[g].size())
      graph[e.to][e.rev].rev = rev;
    graph[s].pop_back();
    graph[g].pop_back();
  }

  int dfs(int v, int t, int f) {
    if (v == t)
      return f;
    use[v] = 1;
    rep(i, graph[v].sz) {
      edge &e = graph[v][i];
      if (!use[e.to] && e.cap > 0) {
        int d = dfs(e.to, t, min(f, e.cap));
        if (d > 0) {
          e.cap -= d;
          graph[e.to][e.rev].cap += d;
          return d;
        }
      }
    }
    return 0;
  }

  int max_flow(int s, int t) {
    int res = 0;
    for (;;) {
      rep(i, n) use[i] = 0;
      int f = dfs(s, t, INF);
      if (!f)
        return res;
      res += f;
    }
  }
};

int main() {
  int n, E, q;
  cin >> n >> E >> q;
  FordFulkerson mf(n);
  rep(i, E) {
    int f, t;
    cin >> f >> t;
    f--;
    t--;
    mf.AddEdge(f, t, 1, 1);
    mf.AddEdge(t, f, 1, 1);
  }

  int F = mf.max_flow(0, n - 1);
  rep(i, q) {
    int m, a, b;
    cin >> m >> a >> b;
    a--;
    b--;
    if (m == 1) {
      mf.AddEdge(a, b, 1, 1);
      mf.AddEdge(b, a, 1, 1);
      F += mf.max_flow(0, n - 1);
    } else {
      int use = 0;
      for (edge e : mf.graph[a]) {
        if (e.to == b && e.cost > 0 && e.cap == 0)
          use |= 1;
      }
      for (edge e : mf.graph[b]) {
        if (e.to == a && e.cost > 0 && e.cap == 0)
          use |= 2;
      }
      // cout << "use: " << use << endl;
      mf.DeleteEdge(a, b);
      mf.DeleteEdge(b, a);

      if (use == 1 || use == 2) {
        rep(i, n) mf.use[i] = 0;
        int another = 0;
        if (use == 1)
          another = mf.dfs(a, b, 1);
        if (use == 2)
          another = mf.dfs(b, a, 1);

        if (!another) {
          rep(i, n) mf.use[i] = 0;
          mf.dfs(use == 1 ? a : b, 0, 1);
          rep(i, n) mf.use[i] = 0;
          mf.dfs(n - 1, use == 1 ? b : a, 1);
          F--;
        }
      }
    }
    // rep(i,n)for(edge e: mf.graph[i])cout << i << " " << e.to << " " << e.cost
    // << " " << e.cap << " " << e.rev <<  endl;

    cout << F << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a) for (int i = 0; i < (int)(a); i++)
#define pb push_back
#define sz size()
using namespace std;
typedef vector<int> vi;

const int INF = 1e9;

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

struct dinic {
  int n;
  vector<vector<edge>> graph;
  vi level, iter;

  dinic(int a = 0) : n(a) { graph.resize(a); }

  void AddEdge(int s, int g, int c, int p) {
    graph[s].pb(edge(s, g, c, p, graph[g].sz));
    graph[g].pb(edge(g, s, -c, 0, graph[s].sz - 1));
  }

  void DeleteEdge(int s, int g) {
    int id = -1, rev = -1;
    rep(e, graph[s].sz) {
      if (graph[s][e].to == g) {
        id = e;
        rev = graph[s][e].rev;
      }
    }
    if (id < 0)
      return;

    swap(graph[s][id], graph[s].back());
    swap(graph[g][rev], graph[g].back());
    graph[s][id].rev = rev;
    graph[g][rev].rev = id;
    graph[s].pop_back();
    graph[g].pop_back();
  }

  void bfs(int s) {
    level = vi(n, -1);
    level[s] = 0;
    queue<int> q;
    q.push(s);
    while (q.sz) {
      int v = q.front();
      q.pop();
      for (edge e : graph[v]) {
        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 < (int)graph[v].sz; i++) {
      edge &e = graph[v][i];
      if (e.cap > 0 && level[v] < level[e.to]) {
        int d = dfs(e.to, t, min(f, e.cap));
        if (d > 0) {
          e.cap -= d;
          graph[e.to][e.rev].cap += d;
          return d;
        }
      }
    }
    return 0;
  }

  int max_flow(int s, int t) {
    int res = 0;
    for (;;) {
      bfs(s);
      if (level[t] < 0)
        return res;
      iter = vi(n, 0);
      int f;
      while ((f = dfs(s, t, INF)) > 0)
        res += f;
    }
  }
};

int main() {
  int n, e, q;
  cin >> n >> e >> q;
  dinic d(n);
  rep(i, e) {
    int f, t;
    cin >> f >> t;
    f--;
    t--;
    d.AddEdge(f, t, 1, 1);
    d.AddEdge(t, f, 1, 1);
  }

  int f = d.max_flow(0, n - 1);
  rep(i, q) {
    int m, a, b;
    cin >> m >> a >> b;
    a--;
    b--;
    if (m == 1) {
      d.AddEdge(a, b, 1, 1);
      d.AddEdge(b, a, 1, 1);
      f += d.max_flow(0, n - 1);
    } else {
      bool use = false;
      for (edge e : d.graph[a]) {
        if (e.to == b && e.cost > 0 && e.cap == 0)
          use = true;
      }
      for (edge e : d.graph[b]) {
        if (e.to == a && e.cost > 0 && e.cap == 0)
          use = true;
      }

      d.DeleteEdge(a, b);
      d.DeleteEdge(b, a);

      if (use) {
        // cerr << a << " " << b << endl;
        rep(i, n) {
          // cerr << i << endl;
          for (edge &e : d.graph[i]) {
            if (e.cost > 0)
              e.cap = 1;
            else
              e.cap = 0;
            // cerr << e.to << " " << e.cost << " " << e.cap << endl;
          }
        }
        f = d.max_flow(0, n - 1);
      }
    }
    cout << f << endl;
  }
}