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

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

#define dump(n) cout << "# " << #n << '=' << (n) << endl
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define peri(i, a, b) for (int i = int(b); i-- > int(a);)
#define rep(i, n) repi(i, 0, n)
#define per(i, n) peri(i, 0, n)
#define all(c) begin(c), end(c)
#define mp make_pair
#define mt make_tuple

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;

const int INF = 1e9;
const int MOD = 1e9 + 7;
const double EPS = 1e-9;

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  return os << '(' << p.first << ',' << p.second << ')';
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &a) {
  os << '[';
  rep(i, a.size()) os << (i ? " " : "") << a[i];
  return os << ']';
}

struct Edge {
  int src, dst, cap, flow;
  Edge() {}
  Edge(int s, int d, int c, int f = 0) : src(s), dst(d), cap(c), flow(f) {}
};
struct Graph {
  vector<Edge> es;
  vi head, next;
  Graph() {}
  Graph(int n) : head(n, -1) {}
  // 有向辺を追加するとき逆辺の容量c2は普通0である．
  // 両方向に容量があるならc2も指定する．
  void AddEdge(int u, int v, int c1, int c2 = 0) {
    es.emplace_back(u, v, c1);
    next.push_back(head[u]);
    head[u] = es.size() - 1;
    es.emplace_back(v, u, c2);
    next.push_back(head[v]);
    head[v] = es.size() - 1;
  }
};

void BFS(const Graph &g, int tap, vi &layer) {
  queue<pii> q;
  q.emplace(tap, 0);
  while (q.size()) {
    int u, d;
    tie(u, d) = q.front();
    q.pop();
    if (layer[u] != INF)
      continue;
    layer[u] = d;
    for (int i = g.head[u]; i != -1; i = g.next[i])
      if (g.es[i].cap - g.es[i].flow > 0)
        q.emplace(g.es[i].dst, d + 1);
  }
}

int DFS(Graph &g, int v, int sink, const vi &layer, int flow) {
  if (v == sink)
    return flow;
  int f = flow;
  for (int &i = g.head[v]; i != -1; i = g.next[i]) {
    Edge &e = g.es[i];
    if (layer[e.src] >= layer[e.dst])
      continue;
    int residue = e.cap - e.flow;
    if (residue == 0)
      continue;
    int augment = DFS(g, e.dst, sink, layer, min(residue, f));
    e.flow += augment;
    g.es[i ^ 1].flow -= augment;
    f -= augment;
    if (f == 0)
      break;
  }
  return flow - f;
}

int Dinic(Graph &g, int tap, int sink) {
  int res = 0;
  for (int n = g.head.size();;) {
    vi layer(n, INF);
    BFS(g, tap, layer);
    if (layer[sink] == INF)
      break;
    vi temp = g.head;
    res += DFS(g, tap, sink, layer, INF);
    swap(g.head, temp);
  }
  return res;
}

bool modify(Graph &g, int v, int sink, int flow, vi &vis) {
  if (v == sink)
    return true;
  if (vis[v])
    return false;
  vis[v] = 1;
  for (int i = g.head[v]; i != -1; i = g.next[i]) {
    Edge e = g.es[i];
    if (e.flow == -flow) {
      if (modify(g, e.dst, sink, flow, vis)) {
        g.es[i].flow = g.es[i ^ 1].flow = 0;
        return true;
      }
    }
  }
  return false;
}

int main() {
  for (int n, m, q; cin >> n >> m >> q && n | m | q;) {
    // dump(vi({n,m,q}));
    Graph g(n);
    rep(i, m) {
      int u, v;
      cin >> u >> v;
      u--, v--;
      g.AddEdge(u, v, 1, 1);
    }
    rep(_, q) {
      int t, u, v;
      cin >> t >> u >> v;
      u--, v--;
      if (t == 1) {
        g.AddEdge(u, v, 1, 1);
      } else {
        int i;
        for (i = g.head[u]; i != -1; i = g.next[i])
          if (g.es[i].dst == v && g.es[i].cap == 1)
            break;
        g.es[i].cap = g.es[i ^ 1].cap = 0;
        if (g.es[i].flow) {
          if (g.es[i].flow == -1)
            swap(u, v);
          g.es[i].flow = g.es[i ^ 1].flow = 0;
          vi vis(n);
          assert(modify(g, v, n - 1, -1, vis));
          fill(all(vis), 0);
          assert(modify(g, u, 0, 1, vis));
        }
      }
      Dinic(g, 0, n - 1);
      // for(int i=0;i<n;i++){
      //	printf("%d:",i);
      //	for(int j=g.head[i];j!=-1;j=g.next[j]){
      //		Edge e=g.es[j];
      //		printf(" (%d,%d/%d)",e.dst,e.flow,e.cap);
      //	}
      //	puts("");
      // }
      int res = 0;
      for (int j = g.head[0]; j != -1; j = g.next[j])
        res += g.es[j].flow;
      cout << res << endl;
    }
  }
}

Error: 0

Fixed submission:

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

#define dump(n) cout << "# " << #n << '=' << (n) << endl
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define peri(i, a, b) for (int i = int(b); i-- > int(a);)
#define rep(i, n) repi(i, 0, n)
#define per(i, n) peri(i, 0, n)
#define all(c) begin(c), end(c)
#define mp make_pair
#define mt make_tuple

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;

const int INF = 1e9;
const int MOD = 1e9 + 7;
const double EPS = 1e-9;

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  return os << '(' << p.first << ',' << p.second << ')';
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &a) {
  os << '[';
  rep(i, a.size()) os << (i ? " " : "") << a[i];
  return os << ']';
}

struct Edge {
  int src, dst, cap, flow;
  Edge() {}
  Edge(int s, int d, int c, int f = 0) : src(s), dst(d), cap(c), flow(f) {}
};
struct Graph {
  vector<Edge> es;
  vi head, next;
  Graph() {}
  Graph(int n) : head(n, -1) {}
  // 有向辺を追加するとき逆辺の容量c2は普通0である．
  // 両方向に容量があるならc2も指定する．
  void AddEdge(int u, int v, int c1, int c2 = 0) {
    es.emplace_back(u, v, c1);
    next.push_back(head[u]);
    head[u] = es.size() - 1;
    es.emplace_back(v, u, c2);
    next.push_back(head[v]);
    head[v] = es.size() - 1;
  }
};

void BFS(const Graph &g, int tap, vi &layer) {
  queue<pii> q;
  q.emplace(tap, 0);
  while (q.size()) {
    int u, d;
    tie(u, d) = q.front();
    q.pop();
    if (layer[u] != INF)
      continue;
    layer[u] = d;
    for (int i = g.head[u]; i != -1; i = g.next[i])
      if (g.es[i].cap - g.es[i].flow > 0)
        q.emplace(g.es[i].dst, d + 1);
  }
}

int DFS(Graph &g, int v, int sink, const vi &layer, int flow) {
  if (v == sink)
    return flow;
  int f = flow;
  for (int &i = g.head[v]; i != -1; i = g.next[i]) {
    Edge &e = g.es[i];
    if (layer[e.src] >= layer[e.dst])
      continue;
    int residue = e.cap - e.flow;
    if (residue == 0)
      continue;
    int augment = DFS(g, e.dst, sink, layer, min(residue, f));
    e.flow += augment;
    g.es[i ^ 1].flow -= augment;
    f -= augment;
    if (f == 0)
      break;
  }
  return flow - f;
}

int Dinic(Graph &g, int tap, int sink) {
  int res = 0;
  for (int n = g.head.size();;) {
    vi layer(n, INF);
    BFS(g, tap, layer);
    if (layer[sink] == INF)
      break;
    vi temp = g.head;
    res += DFS(g, tap, sink, layer, INF);
    swap(g.head, temp);
  }
  return res;
}

bool modify(Graph &g, int p, int v, int sink, int flow) {
  // dump(mp(p,v));
  if (v == sink)
    return true;
  for (int i = g.head[v]; i != -1; i = g.next[i]) {
    if (g.es[i].dst != p && g.es[i].flow == -flow) {
      if (modify(g, g.es[i].src, g.es[i].dst, sink, flow)) {
        g.es[i].flow = g.es[i ^ 1].flow = 0;
        return true;
      }
    }
  }
  return false;
}

int main() {
  for (int n, m, q; cin >> n >> m >> q && n | m | q;) {
    // dump(vi({n,m,q}));
    Graph g(n);
    rep(i, m) {
      int u, v;
      cin >> u >> v;
      u--, v--;
      g.AddEdge(u, v, 1, 1);
    }
    rep(_, q) {
      int t, u, v;
      cin >> t >> u >> v;
      u--, v--;
      if (t == 1) {
        g.AddEdge(u, v, 1, 1);
      } else {
        int i;
        for (i = g.head[u]; i != -1; i = g.next[i])
          if (g.es[i].dst == v && g.es[i].cap == 1)
            break;
        g.es[i].cap = g.es[i ^ 1].cap = 0;
        if (g.es[i].flow) {
          if (g.es[i].flow == -1)
            swap(u, v);
          g.es[i].flow = g.es[i ^ 1].flow = 0;
          assert(modify(g, u, v, n - 1, -1));
          assert(modify(g, v, u, 0, 1));
        }
      }
      Dinic(g, 0, n - 1);
      // for(int i=0;i<n;i++){
      //	printf("%d:",i);
      //	for(int j=g.head[i];j!=-1;j=g.next[j]){
      //		Edge e=g.es[j];
      //		printf(" (%d,%d/%d)",e.dst,e.flow,e.cap);
      //	}
      //	puts("");
      // }
      int res = 0;
      for (int j = g.head[0]; j != -1; j = g.next[j])
        res += g.es[j].flow;
      cout << res << endl;
    }
  }
}