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

#include <bits/stdc++.h>
#define fi first
#define sc second
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()
#define cmin(a, b) (a > b ? a = b : a)
#define cmax(a, b) (a < b ? a = b : a)
#define mem(a, k) memset(a, k, sizeof(a))
#define lop(i, s, t) for (int i = s; i < (t); ++i)
#define rep(i, s, t) for (int i = s; i <= (t); ++i)
#define dec(i, s, t) for (int i = s; i >= (t); --i)
#define fore(i, v)                                                             \\
  for (int i = g[v], d = es[i].d; i; i = es[i].nxt, d = es[i].d)
using namespace std;

#ifdef CURIOUSCAT
#define Pr(f, ...) fprintf(stderr, f, ##__VA_ARGS__), fflush(stderr)
#else
#define Pr(f, ...)
#endif

typedef long long ll;

template <typename T> void read(T &x) {
  x = 0;
  char c;
  for (c = getchar(); !isdigit(c); c = getchar())
    ;
  for (; isdigit(c); c = getchar())
    x = x * 10 + c - '0';
}

const int N = 1e5 + 50;
const int oo = 1e9 + 7;

int n, a[N], gd_c0, b[N], ans, c[N], c0, X, P, m, ind[N], oud[N], sz[N], bl[N],
    cc, cir[N];
vector<int> g[N], c1, c2;
void dfs(int v) {
  bl[v] = cc;
  ++sz[cc];
  cir[cc] &= ind[v] == oud[v] && ind[v] == 1;
  for (int d : g[v])
    if (!bl[d])
      dfs(d);
}

int main(int argc, char *argv[]) {
#ifdef CURIOUSCAT
  freopen("d.in", "r", stdin);
#endif
  read(n), read(m);
  rep(i, 1, m) {
    read(a[i]), read(b[i]), read(c[i]);
    g[a[i]].pb(b[i]), g[b[i]].pb(a[i]);
    ++ind[b[i]], ++oud[a[i]];
  }
  rep(i, 1, n) if (!bl[i]) {
    ++cc;
    cir[cc] = 1;
    dfs(i);
    if (cir[cc] && sz[cc] > 1)
      ++X;
  }
  rep(i, 1, m) {
    int u = bl[a[i]];
    if (a[i] != b[i]) {
      ++P;
      c0 += c[i] - 1;
      gd_c0 |= c[i] > 1 && (!cir[u]);
    } else {
      if (cir[u] && sz[u] == 1) {
        if (c[i] >= 2)
          c2.pb(c[i] - 2);
      } else if (c[i] > 1)
        c1.pb(c[i] - 1);
    }
  }
  // ans=P+X+2Y+Z,st wc0+wc1+wc2>=x
  Pr("%d,%d,%d,%d,%d\\n", P, X, c0, (int)c1.size(), (int)c2.size());
  if (X && (!c1.size() && !gd_c0)) {
    cout << -1 << endl;
    return 0;
  }
  if (c0 >= X) {
    cout << P + X << endl;
  } else {
    ans = oo;
    sort(all(c1), greater<int>());
    sort(all(c2));
    int p = 0, cw = accumulate(all(c2), 0) + c0;
    for (int i = 0; i <= c1.size(); ++i) {
      if (cw < X)
        ;
      else {
        while (p != c2.size() && cw - c2[p] >= X)
          cw -= c2[p], ++p;
        if (gd_c0 || i)
          cmin(ans, 1 * i + 2 * ((int)c2.size() - p));
      }
      if (i < c1.size())
        cw += c1[i];
    }
    cout << (ans == oo ? -1 : P + X + ans) << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define fi first
#define sc second
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()
#define cmin(a, b) (a > b ? a = b : a)
#define cmax(a, b) (a < b ? a = b : a)
#define mem(a, k) memset(a, k, sizeof(a))
#define lop(i, s, t) for (int i = s; i < (t); ++i)
#define rep(i, s, t) for (int i = s; i <= (t); ++i)
#define dec(i, s, t) for (int i = s; i >= (t); --i)
#define fore(i, v)                                                             \\
  for (int i = g[v], d = es[i].d; i; i = es[i].nxt, d = es[i].d)
using namespace std;

#ifdef CURIOUSCAT
#define Pr(f, ...) fprintf(stderr, f, ##__VA_ARGS__), fflush(stderr)
#else
#define Pr(f, ...)
#endif

typedef long long ll;

template <typename T> void read(T &x) {
  x = 0;
  char c;
  for (c = getchar(); !isdigit(c); c = getchar())
    ;
  for (; isdigit(c); c = getchar())
    x = x * 10 + c - '0';
}

const int N = 1e5 + 50;
const int oo = 1e9 + 7;

int n, a[N], gd_c0, b[N], ans, c[N], c0, X, P, m, ind[N], oud[N], sz[N], bl[N],
    cc, cir[N];
vector<int> g[N], c1, c2;
void dfs(int v) {
  bl[v] = cc;
  ++sz[cc];
  cir[cc] &= ind[v] == oud[v] && ind[v] == 1;
  for (int d : g[v])
    if (!bl[d])
      dfs(d);
}

int main(int argc, char *argv[]) {
#ifdef CURIOUSCAT
  freopen("d.in", "r", stdin);
#endif
  read(n), read(m);
  rep(i, 1, m) {
    read(a[i]), read(b[i]), read(c[i]);
    g[a[i]].pb(b[i]), g[b[i]].pb(a[i]);
    ++ind[b[i]], ++oud[a[i]];
  }
  rep(i, 1, n) if (!bl[i]) {
    ++cc;
    cir[cc] = 1;
    dfs(i);
    if (cir[cc] && sz[cc] > 1)
      ++X;
  }
  rep(i, 1, m) {
    int u = bl[a[i]];
    if (a[i] != b[i]) {
      ++P;
      c0 += c[i] - 1;
    } else {
      if (cir[u] && sz[u] == 1) {
        if (c[i] >= 2)
          c2.pb(c[i] - 2);
      } else if (c[i] > 1)
        c1.pb(c[i] - 1);
      gd_c0 |= c[i] > 1 && (!cir[u]);
    }
  }
  // ans=P+X+2Y+Z,st wc0+wc1+wc2>=x
  if (X && (!c0 && !gd_c0)) {
    cout << -1 << endl;
    return 0;
  }
  if (c0 >= X) {
    cout << P + X << endl;
  } else {
    ans = oo;
    sort(all(c1), greater<int>());
    sort(all(c2));
    int p = 0, cw = accumulate(all(c2), 0) + c0;
    for (int i = 0; i <= c1.size(); ++i) {
      if (cw < X)
        ;
      else {
        while (p != c2.size() && cw - c2[p] >= X)
          cw -= c2[p], ++p;
        if (c0 || i)
          cmin(ans, 1 * i + 2 * ((int)c2.size() - p));
      }
      cw += c1[i];
    }
    cout << (ans == oo ? -1 : P + X + ans) << endl;
  }
  return 0;
}