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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define loop(i, a, b) for (ll i = (a); i < ll(b); i++)
#define rep(i, b) loop(i, 0, b)

typedef int Capacity;
struct Edge {
  int src, dst;
  Capacity cap;
  Edge(int src_, int dst_, Capacity cap_) : src(src_), dst(dst_), cap(cap_) {}
};

typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
struct Dinic {
  int n, s, t;
  vector<int> level, prog, que;
  vector<vector<Capacity>> cap, flow;
  vector<vector<int>> g;
  Capacity inf;
  Dinic(int n)
      : n(n), cap(n, vector<Capacity>(n)), flow(n, vector<Capacity>(n)),
        g(n, vector<int>()), inf(numeric_limits<Capacity>::max() / 8) {}
  Dinic(const Graph &graph) {
    *this = Dinic(graph.size());
    for (int i = 0; i < n; i++)
      for (auto &e : graph[i])
        addEdge(e.src, e.dst, e.cap);
  }
  void addEdge(int u, int v, Capacity c) {
    cap[u][v] += c;
    cap[v][u] += c;
    flow[v][u] += c;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  inline Capacity residue(int u, int v) { return cap[u][v] - flow[u][v]; }
  Capacity solve(int s, int t) {
    this->t = t, this->s = s;
    que.resize(n + 1);
    Capacity res = 0;
    while (levelize()) {
      prog.assign(n, 0);
      res += augment(s, inf);
    }
    return res;
  }
  bool levelize() {
    int l = 0, r = 0;
    level.assign(n, -1);
    level[s] = 0;
    que[r++] = s;
    while (l != r) {
      int v = que[l++];
      if (v == t)
        break;
      for (const int &d : g[v]) {
        if (level[d] != -1 || residue(v, d) == 0)
          continue;
        level[d] = level[v] + 1;
        que[r++] = d;
      }
    }
    return level[t] != -1;
  }
  Capacity augment(int v, Capacity lim) {
    Capacity res = 0;
    if (v == t)
      return lim;
    for (int &i = prog[v]; i < (int)g[v].size(); i++) {
      const int &d = g[v][i];
      if (residue(v, d) == 0 || level[v] >= level[d])
        continue;
      const Capacity aug = augment(d, min(lim, residue(v, d)));
      flow[v][d] += aug;
      flow[d][v] -= aug;
      res += aug;
      lim -= aug;
      if (lim == 0)
        break;
    }
    return res;
  }
};

int main() {
  int N, M;
  cin >> N >> M;
  Graph g(N);
  rep(i, M) {
    int a, b, c;
    cin >> a >> b >> c;
    g[a].emplace_back(a, b, c);
  }
  Dinic ek(g);
  cout << ek.solve(0, N - 1) << endl;
}

Error: -6
double free or corruption (out)


Fixed submission:

#include <algorithm>
#include <cassert>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

struct LowestCommonAncestor {
  size_t n;
  size_t size() const { return n; }
  vector<int> color, parent, depth, topDepth, goUp, visited;
  vector<vector<int>> g;
  LowestCommonAncestor(size_t n_) : n(n_), g(n) {}
  void addEdge(int u, int v) {
    g[u].push_back(v);
    g[v].push_back(u);
  }
  void decomp(int root = 0, int B = 400) {
    parent.assign(n, -1);
    depth.assign(n, 0);
    topDepth.assign(n, 0);
    goUp.assign(n, -1);
    visited.assign(n, false);
    vector<int> tord;
    vector<int> q(n + 1);
    int l = 0, r = 0;
    q[r++] = root;
    while (l != r) {
      int v = q[l++];
      visited[v] = true;
      tord.push_back(v);
      for (int c : g[v]) {
        if (!visited[c])
          parent[c] = v;
        depth[c] = depth[v] + 1;
        q[r++] = c;
      }
    }
    swap(color, visited); // ?????¢????????±??????
    color.assign(n, 0);
    int c = 0;
    for (int u : tord) {
      if (color[u])
        continue;
      c++;
      l = 0, r = 0;
      q[r++] = u;
      for (int i = 0; i < B && l != r; i++) {
        int v = q[l++];
        color[v] = c;
        goUp[v] = parent[u];
        topDepth[v] = depth[u];
        for (int c : g[v])
          if (!color[c])
            q[r++] = c;
      }
    }
  }
  int solve(int u, int v) {
    while (color[u] != color[v]) {
      if (topDepth[u] > topDepth[v])
        u = goUp[u];
      else
        v = goUp[v];
    }
    while (u != v) {
      if (depth[u] > depth[v])
        u = parent[u];
      else
        v = parent[v];
    }
    return u;
  }
};

#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main() {
  int n;
  cin >> n;
  LowestCommonAncestor g(n);
  rep(i, n) {
    int k;
    cin >> k;
    rep(j, k) {
      int c;
      cin >> c;
      g.addEdge(i, c);
    }
  }
  g.decomp(0);
  int q;
  cin >> q;
  rep(i, q) {
    int u, v;
    cin >> u >> v;
    cout << g.solve(u, v) << endl;
  }
}