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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define dbg(...)                                                               \\
  do {                                                                         \\
    cerr << __LINE__ << ": ";                                                  \\
    dbgprint(#__VA_ARGS__, __VA_ARGS__);                                       \\
  } while (0);

using namespace std;

namespace std {
template <class S, class T> struct hash<pair<S, T>> {
  size_t operator()(const pair<S, T> &p) const {
    return ((size_t)1e9 + 7) * hash<S>()(p.first) + hash<T>()(p.second);
  }
};
template <class T> struct hash<vector<T>> {
  size_t operator()(const vector<T> &v) const {
    size_t h = 0;
    for (auto i : v)
      h = h * ((size_t)1e9 + 7) + hash<T>()(i) + 1;
    return h;
  }
};
} // namespace std
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "[ ";
  rep(i, v.size()) os << v[i] << (i == v.size() - 1 ? " ]" : ", ");
  return os;
}
template <class T> ostream &operator<<(ostream &os, const set<T> &v) {
  os << "{ ";
  for (const auto &i : v)
    os << i << ", ";
  return os << "}";
}
template <class T, class U>
ostream &operator<<(ostream &os, const map<T, U> &v) {
  os << "{";
  for (const auto &i : v)
    os << " " << i.first << ": " << i.second << ",";
  return os << "}";
}
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  return os << "(" << p.first << ", " << p.second << ")";
}
void dbgprint(const string &fmt) { cerr << endl; }
template <class H, class... T>
void dbgprint(const string &fmt, const H &h, const T &...r) {
  cerr << fmt.substr(0, fmt.find(",")) << "= " << h << " ";
  dbgprint(fmt.substr(fmt.find(",") + 1), r...);
}
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;

vector<vector<pi>> build_graph(int n, vi &x, vi &y, vi &z, int s) {
  vector<vector<pi>> e(n);
  auto is = [&](int i, int j) {
    int dx = s - abs(x[i] - x[j]);
    int dy = s - abs(y[i] - y[j]);
    int dz = s - abs(z[i] - z[j]);
    if (dx <= 0 || dy <= 0 || dz <= 0)
      return 0;
    return 2 * (dx * dy + dy * dz + dz * dx);
  };
  rep(i, n) rep(j, i) {
    int x = is(i, j);
    if (!x)
      continue;
    e[i].emplace_back(j, x);
    e[j].emplace_back(i, x);
    // dbg(i, j, x);
  }
  return e;
}
vector<pair<bool, vi>> composition(vector<vector<pi>> &e, int n) {
  vector<bool> v(n);
  vector<pair<bool, vi>> res;
  function<bool(int, int, vi &)> rec = [&](int c, int p, vi &vs) {
    vs.pb(c);
    v[c] = 1;
    bool res = 0;
    for (auto i : e[c])
      if (i.first != p) {
        if (v[i.first])
          res = 1;
        else
          res |= rec(i.first, c, vs);
      }
    return res;
  };
  rep(i, n) if (!v[i]) {
    vi vs;
    bool lp = rec(i, i, vs);
    res.emplace_back(lp, vs);
    // dbg(lp, vs);
  }
  return res;
}
ll calc_loop(vi &vs, vector<vector<pi>> &e) {
  ll sum = 0;
  for (int p = -1, c = vs[0], i = 0; i < vs.size(); i++) {
    for (auto j : e[c])
      if (j.first != p) {
        sum += j.second;
        p = c;
        c = j.first;
        break;
      }
  }
  return sum;
}
vi calc_cost(bool loop, vi &vs, vector<vector<pi>> &e) {
  vi cost;
  int c = vs[0];
  for (int i : vs)
    if (e[i].size() == 1)
      c = i;

  for (int p = -1, i = 0; i < (int)vs.size() - !loop; i++) {
    int nc = -1;
    for (auto j : e[c])
      if (j.first != p) {
        cost.pb((int)j.second);
        p = c;
        nc = j.first;
        break;
      }
    assert(nc != -1);
    c = nc;
  }
  if (loop) {
    // dbg(cost);
    rep(i, vs.size()) cost.pb(cost[i]);
    // dbg(cost);
  }
  return cost;
}
int main() {
  cin.tie(0);
  cin.sync_with_stdio(0);
  int n, k, s;
  while (cin >> n >> k >> s, n) {
    vi x(n), y(n), z(n);
    rep(i, n) cin >> x[i] >> y[i] >> z[i];

    if (k == 1) {
      cout << 6ll * k * s * s << endl;
      continue;
    }

    vector<vector<pi>> e = build_graph(n, x, y, z, s);
    vector<pair<bool, vi>> comp = composition(e, n);
    ll mx = -1;

    for (auto c : comp)
      if (c.second.size() >= k) {
        bool loop = c.first;
        vi vs = c.second;
        if (loop && vs.size() == k)
          mx = max(mx, calc_loop(vs, e));
        else {
          vi costs = calc_cost(loop, vs, e);
          /*
          ll sum = accumulate(costs.begin(), costs.begin() + k - 2, 0ll);
          rep(i, (int)costs.size() - k + 2){
                  sum += costs[i + k - 2];
                  mx = max(mx, sum);
                  sum -= costs[i];
          }
          dbg(loop, 6 * k * s * s);
          dbg(k, costs)
          */
          ;
          rep(j, costs.size()) {
            ll sum = 0;
            rep(i, k - 1) if (j + i < costs.size()) sum += costs[j + i];
            else {
              sum = -1;
              break;
            }
            // dbg(sum, mx);
            mx = max(mx, sum);
          }
        }
      }
    cout << (mx < 0 ? -1ll : 6ll * k * s * s - mx) << endl;
  }
  return 0;
}

Error: -6
4c6d1141-78a6-4713-82b5-d4a04c97399b.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p01097/C++/s927839534.cpp:85: int main(): Assertion `e[idx[i]].size() >= 1' failed.


Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define dbg(...)                                                               \\
  do {                                                                         \\
    cerr << __LINE__ << ": ";                                                  \\
    dbgprint(#__VA_ARGS__, __VA_ARGS__);                                       \\
  } while (0);

using namespace std;

namespace std {
template <class S, class T> struct hash<pair<S, T>> {
  size_t operator()(const pair<S, T> &p) const {
    return ((size_t)1e9 + 7) * hash<S>()(p.first) + hash<T>()(p.second);
  }
};
template <class T> struct hash<vector<T>> {
  size_t operator()(const vector<T> &v) const {
    size_t h = 0;
    for (auto i : v)
      h = h * ((size_t)1e9 + 7) + hash<T>()(i) + 1;
    return h;
  }
};
} // namespace std
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "[ ";
  rep(i, v.size()) os << v[i] << (i == v.size() - 1 ? " ]" : ", ");
  return os;
}
template <class T> ostream &operator<<(ostream &os, const set<T> &v) {
  os << "{ ";
  for (const auto &i : v)
    os << i << ", ";
  return os << "}";
}
template <class T, class U>
ostream &operator<<(ostream &os, const map<T, U> &v) {
  os << "{";
  for (const auto &i : v)
    os << " " << i.first << ": " << i.second << ",";
  return os << "}";
}
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  return os << "(" << p.first << ", " << p.second << ")";
}
void dbgprint(const string &fmt) { cerr << endl; }
template <class H, class... T>
void dbgprint(const string &fmt, const H &h, const T &...r) {
  cerr << fmt.substr(0, fmt.find(",")) << "= " << h << " ";
  dbgprint(fmt.substr(fmt.find(",") + 1), r...);
}
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;

int main() {
  cin.tie(0);
  cin.sync_with_stdio(0);
  int n, k, s;
  while (cin >> n >> k >> s, n) {
    vi x(n), y(n), z(n);
    rep(i, n) cin >> x[i] >> y[i] >> z[i];

    auto intersect = [&](int i, int j) {
      ll dx = s - abs(x[i] - x[j]);
      ll dy = s - abs(y[i] - y[j]);
      ll dz = s - abs(z[i] - z[j]);
      if (dx <= 0 || dy <= 0 || dz <= 0)
        return 0ll;
      return (dx * dy + dy * dz + dz * dx) * 2;
    };

    vector<vi> e(n);
    rep(i, n) rep(j, i) if (intersect(i, j)) {
      e[i].pb(j);
      e[j].pb(i);
      // dbg(i, j);
    }

    function<bool(int, int, vector<bool> &, vi &)> rec =
        [&](int c, int p, vector<bool> &v, vi &idx) {
          v[c] = 1;
          bool ans = 0;
          idx.pb(c);
          for (int i : e[c])
            if (i != p) {
              if (v[i])
                ans |= 1;
              else
                ans |= rec(i, c, v, idx);
            }
          return ans;
        };

    ll ans = 1e18;
    vector<bool> v(n);
    rep(ii, n) if (!v[ii]) {
      vi idx;
      bool loop = rec(ii, ii, v, idx);
      if (idx.size() < k)
        continue;

      if (loop && idx.size() == k) {
        ll sum = 6ll * k * s * s;
        rep(i, (int)idx.size()) {
          ll c = intersect(idx[i], idx[(i + 1) % idx.size()]);
          sum -= c;
        }
        ans = min(ans, sum);
      } else {
        vector<ll> costs;
        if (loop) {
          rep(i, (int)idx.size())
              costs.pb(intersect(idx[i], idx[(i + 1) % idx.size()]));
          int it = idx.size();
          rep(i, it) idx.pb(idx[i]);
        } else {
          int hasi = 0;
          rep(i, idx.size()) if (e[idx[i]].size() == 1) hasi = idx[i];
          for (int i : idx)
            v[i] = 0;
          idx.clear();
          rec(hasi, hasi, v, idx);

          rep(i, (int)idx.size() - 1) costs.pb(intersect(idx[i], idx[i + 1]));

          rep(i, idx.size()) {
            // if(i==0 || i==idx.size()-1) assert(e[idx[i]].size() <= 1);
            // else assert(e[idx[i]].size()==2);
            assert(e[idx[i]].size() <= 2);
            assert(e[idx[i]].size() >= 1);
          }
        }
        // dbg(k, costs);

        ll sum = 6ll * k * s * s;
        if (k == 1) {
          ans = min(ans, sum);
          continue;
        }

        rep(i, k - 2) sum -= costs[i];
        rep(i, (int)costs.size() - (k - 1) + 1) {
          sum -= costs[i + k - 2];
          ans = min(ans, sum);
          sum += costs[i];
        }
      }
    };
    if (ans >= 1e18)
      ans = -1;
    cout << ans << endl;
  }
  return 0;
}