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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define mind(a, b) (a > b ? b : a)
#define maxd(a, b) (a > b ? a : b)
#define absd(x) (x < 0 ? -(x) : x)
#define pow2(x) ((x) * (x))
#define rep(i, n) for (int i = 0; i < n; ++i)
#define repr(i, n) for (int i = n - 1; i >= 0; --i)
#define repl(i, s, n) for (int i = s; i <= n; ++i)
#define replr(i, s, n) for (int i = n; i >= s; --i)
#define repf(i, s, n, j) for (int i = s; i <= n; i += j)
#define repe(e, obj) for (auto e : obj)

#define SP << " " <<
#define COL << " : " <<
#define COM << ", " <<
#define ARR << " -> " <<
#define PNT(STR) cout << STR << endl
#define POS(X, Y) "(" << X << ", " << Y << ")"
#define DEB(A) " (" << #A << ") " << A
#define DEBREP(i, n, val)                                                      \\
  for (int i = 0; i < n; ++i)                                                  \\
    cout << val << " ";                                                        \\
  cout << endl
#define ALL(V) (V).begin(), (V).end()
#define INF 1000000007
#define INFLL 10000000000000000007LL
#define EPS 1e-9

typedef unsigned int uint;
typedef unsigned long ulong;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
// typedef pair<ll, ll> P;
typedef pair<P, int> PI;
typedef pair<int, P> IP;
typedef pair<P, P> PP;
typedef priority_queue<P, vector<P>, greater<P>> pvqueue;

struct Edge {
  int to, cap, rev, m_cap;
  Edge(int t, int c, int r) : to(t), cap(c), m_cap(c), rev(r) {}
  Edge() {}
};

#define MAX_N 1005

class Dinic {
  vector<Edge> g[MAX_N];
  int level[MAX_N], iter[MAX_N];
  int dist[MAX_N];
  int flow, n;
  int st, en;
  map<P, int> edges;

  void bfs(int s) {
    rep(i, n) level[i] = -1, dist[i] = INF;
    queue<int> que;
    level[s] = 0;
    que.push(s);
    while (!que.empty()) {
      int v = que.front();
      que.pop();
      rep(i, g[v].size()) {
        Edge &e = g[v][i];
        if (e.cap > 0 && level[e.to] < 0) {
          level[e.to] = level[v] + 1;
          dist[e.to] = mind(e.cap, dist[v]);
          que.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 (e.cap > 0 && level[v] < level[e.to]) {
        int d = dfs(e.to, t, mind(f, e.cap));
        if (d > 0) {
          e.cap -= d;
          g[e.to][e.rev].cap += d;
          return d;
        }
      }
    }
    return 0;
  }

public:
  Dinic(int n, int s, int t) {
    flow = 0;
    st = s;
    en = t;
    this->n = n;
  }

  // ??????????????????
  void add_edge(int from, int to, int cap) {
    edges[P(from, to)] = g[from].size();
    g[from].push_back(Edge(to, cap, g[to].size()));
    edges[P(to, from)] = g[to].size();
    g[to].push_back(Edge(from, 0, g[from].size() - 1));
  }

  // ??????????????????
  void add_multi_edge(int v1, int v2, int cap) {
    edges[P(v1, v2)] = g[v1].size();
    g[v1].push_back(Edge(v2, cap, g[v2].size()));
    edges[P(v2, v1)] = g[v2].size();
    g[v2].push_back(Edge(v1, cap, g[v1].size() - 1));
  }

  // ?????§???????±???????
  int max_flow(int s = -1, int t = -1, int rest = -1) {
    // cout << "----" SP s SP t SP rest << endl;
    if (s == -1)
      s = st;
    if (t == -1)
      t = en;
    int su = 0;
    while (rest) {
      bfs(s);
      if (level[t] < 0)
        break;
      rep(i, n) iter[i] = 0;
      int f;
      // cout << "e" SP rest << endl;
      while (rest && (f = dfs(s, t, rest == -1 ? INF : rest)) > 0) {
        if (rest == -1) {
          flow += f;
        } else {
          rest -= f;
        }
        su += f;
      }
    }
    return rest == -1 ? flow : su;
  }

  // ???????¢????
  void inc_edge(int s, int t, int c) {
    // cout << "inc_edge" SP s SP t SP c << endl;
    P key = P(s, t);
    if (edges.find(key) != edges.end()) {
      g[s][edges[key]].cap += c;
      g[s][edges[key]].m_cap += c;
    } else {
      add_edge(s, t, c);
    }
  }

  // ??????????°?
  void dec_edge(int s, int t, int c) {
    // cout << "dec_edge" SP s SP t SP c << endl;
    P key = P(s, t);
    if (edges.find(key) != edges.end()) {
      int cap = g[s][edges[key]].cap;
      if (c <= cap) {
        g[s][edges[key]].cap -= c;
      } else {
        int rest = c - cap;

        // cout << "d" SP c SP cap SP g[t][edges[P(t, s)]].cap << endl;
        // cout << "m" SP g[s][edges[P(s, t)]].m_cap SP g[t][edges[P(t,
        // s)]].m_cap << endl;
        g[s][edges[key]].cap = 0;
        g[t][edges[P(t, s)]].cap -= rest;

        bfs(s);
        // cout << level[t] << endl;
        if (level[t] >= 0) {
          int ff = max_flow(s, t, rest);
          rest -= ff;
        }

        if (rest) {
          int f1 = max_flow(en, t, rest);
          int f2 = max_flow(s, st, rest);
          // cout << f1 SP f2 << endl;

          assert(f1 == f2 && f1 == rest && f2 == rest);
          flow -= rest;
        }
      }
      g[s][edges[key]].m_cap -= c;
    }
  }

  void debug() {
    rep(i, n) {
      rep(j, n) {
        if (i == j)
          continue;
        if (edges.find(P(i, j)) != edges.end()) {
          Edge &e = g[i][edges[P(i, j)]];
          cout << i SP j SP e.cap << endl;
        }
      }
    }
  }
};

int main() {
  int n, e, q;
  cin >> n >> e >> q;
  Dinic dinic(n, 0, n - 1);
  rep(i, e) {
    int f, t;
    cin >> f >> t;
    --f;
    --t;
    dinic.add_multi_edge(f, t, 1);
  }
  // cout << dinic.max_flow() << endl;
  rep(i, q) {
    int m, a, b;
    cin >> m >> a >> b;
    --a;
    --b;
    // dinic.debug();
    if (m == 1) {
      dinic.inc_edge(a, b, 1);
      dinic.inc_edge(b, a, 1);
    } else {
      dinic.dec_edge(a, b, 1);
      dinic.dec_edge(b, a, 1);
    }
    int f = dinic.max_flow();
    cout << f << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define mind(a, b) (a > b ? b : a)
#define maxd(a, b) (a > b ? a : b)
#define absd(x) (x < 0 ? -(x) : x)
#define pow2(x) ((x) * (x))
#define rep(i, n) for (int i = 0; i < n; ++i)
#define repr(i, n) for (int i = n - 1; i >= 0; --i)
#define repl(i, s, n) for (int i = s; i <= n; ++i)
#define replr(i, s, n) for (int i = n; i >= s; --i)
#define repf(i, s, n, j) for (int i = s; i <= n; i += j)
#define repe(e, obj) for (auto e : obj)

#define SP << " " <<
#define COL << " : " <<
#define COM << ", " <<
#define ARR << " -> " <<
#define PNT(STR) cout << STR << endl
#define POS(X, Y) "(" << X << ", " << Y << ")"
#define DEB(A) " (" << #A << ") " << A
#define DEBREP(i, n, val)                                                      \\
  for (int i = 0; i < n; ++i)                                                  \\
    cout << val << " ";                                                        \\
  cout << endl
#define ALL(V) (V).begin(), (V).end()
#define INF 1000000007
#define INFLL 10000000000000000007LL
#define EPS 1e-9

typedef unsigned int uint;
typedef unsigned long ulong;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
// typedef pair<ll, ll> P;
typedef pair<P, int> PI;
typedef pair<int, P> IP;
typedef pair<P, P> PP;
typedef priority_queue<P, vector<P>, greater<P>> pvqueue;

struct Edge {
  int to, cap, rev;
  Edge(int t, int c, int r) : to(t), cap(c), rev(r) {}
  Edge() {}
};

#define MAX_N 1005

class Dinic {
  vector<Edge> g[MAX_N];
  int level[MAX_N], iter[MAX_N];
  int flow, n;
  int st, en;
  map<P, int> edges;

  void bfs(int s) {
    rep(i, n) level[i] = -1;
    queue<int> que;
    level[s] = 0;
    que.push(s);
    while (!que.empty()) {
      int v = que.front();
      que.pop();
      rep(i, g[v].size()) {
        Edge &e = g[v][i];
        if (e.cap > 0 && level[e.to] < 0) {
          level[e.to] = level[v] + 1;
          que.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 (e.cap > 0 && level[v] < level[e.to]) {
        int d = dfs(e.to, t, mind(f, e.cap));
        if (d > 0) {
          e.cap -= d;
          g[e.to][e.rev].cap += d;
          return d;
        }
      }
    }
    return 0;
  }

public:
  Dinic(int n, int s, int t) {
    flow = 0;
    st = s;
    en = t;
    this->n = n;
  }

  // ??????????????????
  void add_edge(int from, int to, int cap) {
    edges[P(from, to)] = g[from].size();
    g[from].push_back(Edge(to, cap, g[to].size()));
    edges[P(to, from)] = g[to].size();
    g[to].push_back(Edge(from, 0, g[from].size() - 1));
  }

  // ??????????????????
  void add_multi_edge(int v1, int v2, int cap) {
    edges[P(v1, v2)] = g[v1].size();
    g[v1].push_back(Edge(v2, cap, g[v2].size()));
    edges[P(v2, v1)] = g[v2].size();
    g[v2].push_back(Edge(v1, cap, g[v1].size() - 1));
  }

  // ?????§???????±???????
  int max_flow(int s = -1, int t = -1, int rest = -1) {
    if (s == -1)
      s = st;
    if (t == -1)
      t = en;
    int su = 0;
    while (rest) {
      bfs(s);
      if (level[t] < 0)
        return flow;
      rep(i, n) iter[i] = 0;
      int f;
      while (rest && (f = dfs(s, t, rest == -1 ? INF : rest)) > 0) {
        if (rest == -1) {
          flow += f;
        } else {
          rest -= f;
        }
        su += f;
      }
    }
    return rest == -1 ? flow : su;
  }

  // ???????¢????
  void inc_edge(int s, int t, int c) {
    P key = P(s, t);
    if (edges.find(key) != edges.end()) {
      g[s][edges[key]].cap += c;
    } else {
      add_edge(s, t, c);
    }
  }

  // ??????????°?
  void dec_edge(int s, int t, int c) {
    P key = P(s, t);
    if (edges.find(key) != edges.end()) {
      int cap = g[s][edges[key]].cap;
      if (c <= cap) {
        g[s][edges[key]].cap -= c;
      } else {
        int rest = c - cap;
        // en -> t
        int f1 = max_flow(en, t, rest);
        // s -> st
        int f2 = max_flow(s, st, rest);

        assert(f1 == rest && f2 == rest);

        g[s][edges[key]].cap = 0;
        g[t][edges[P(t, s)]].cap -= rest;
        // cout << g[s][edges[key]].cap << endl;

        flow -= c;
      }
    }
  }
};

int main() {
  int n, e, q;
  cin >> n >> e >> q;
  Dinic dinic(n, 0, n - 1);
  rep(i, e) {
    int f, t;
    cin >> f >> t;
    --f;
    --t;
    dinic.add_multi_edge(f, t, 1);
  }
  // cout << dinic.max_flow() << endl;
  rep(i, q) {
    int m, a, b;
    cin >> m >> a >> b;
    --a;
    --b;
    if (m == 1) {
      dinic.inc_edge(a, b, 1);
      dinic.inc_edge(b, a, 1);
    } else {
      dinic.dec_edge(a, b, 1);
      dinic.dec_edge(b, a, 1);
    }
    int f = dinic.max_flow();
    cout << f << endl;
  }
  return 0;
}