Problem p03143 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using i64 = long long;

template <class T = int> class UF {
  T *par;

public:
  UF(T n) : par(new T[n]) {
    for (T i = 0; i < n; i++) {
      par[i] = i;
    }
  }
  ~UF() { delete[] par; }

  T root(T x) {
    if (par[x] == x)
      return x;
    return par[x] = root(par[x]);
  }

  bool same(T x, T y) { return root(x) == root(y); }

  void unite(T x, T y) {
    x = root(x);
    y = root(y);
    if (x != y) {
      par[x] = y;
    }
  }
};

int main() {
  int n, m;
  std::cin >> n >> m;
  std::vector<i64> w(n);
  for (int i = 0; i < n; i++) {
    std::cin >> w[i];
  }

  std::vector<std::pair<i64, std::pair<int, int>>> es;
  for (int i = 0; i < m; i++) {
    int a, b;
    i64 y;
    std::cin >> a >> b >> y;
    a--;
    b--;
    es.push_back({y, {a, b}});
  }

  std::sort(es.begin(), es.end());
  UF<> uft(n);
  std::vector<bool> cand(m), done(m);
  std::vector<std::deque<std::pair<int, int>>> g(n);
  for (int i = 0; i < m; i++) {
    auto a = es[i].second.first, b = es[i].second.second;
    auto y = es[i].first;
    auto xx = w[uft.root(a)] + (uft.same(a, b) ? 0 : w[uft.root(b)]);
    uft.unite(a, b);
    w[uft.root(a)] = xx;
    if (y <= xx) {
      cand[i] = true;
    }
    g[a].emplace_back(b, i);
    g[b].emplace_back(a, i);
  }

  int ret = 0;
  for (int i = m - 1; i >= 0; i--) {
    if (done[i])
      continue;
    if (!cand[i]) {
      ret++;
      continue;
    }
    std::deque<int> q{es[i].second.first, es[i].second.second};
    while (!q.empty()) {
      auto v = q.front();
      q.pop_front();
      for (auto el : g[v]) {
        if (!done[el.second] && es[el.second].first <= es[i].first) {
          q.push_back(el.second);
          done[el.second] = true;
        }
      }
    }
  }

  std::cout << ret << std::endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using i64 = long long;

template <class T = int> class UF {
  T *par;

public:
  UF(T n) : par(new T[n]) {
    for (T i = 0; i < n; i++) {
      par[i] = i;
    }
  }
  ~UF() { delete[] par; }

  T root(T x) {
    if (par[x] == x)
      return x;
    return par[x] = root(par[x]);
  }

  bool same(T x, T y) { return root(x) == root(y); }

  void unite(T x, T y) {
    x = root(x);
    y = root(y);
    if (x != y) {
      par[x] = y;
    }
  }
};

int main() {
  int n, m;
  std::cin >> n >> m;
  std::vector<i64> w(n);
  for (int i = 0; i < n; i++) {
    std::cin >> w[i];
  }

  std::vector<std::pair<i64, std::pair<int, int>>> es;
  for (int i = 0; i < m; i++) {
    int a, b;
    i64 y;
    std::cin >> a >> b >> y;
    a--;
    b--;
    es.push_back({y, {a, b}});
  }

  std::sort(es.begin(), es.end());
  UF<> uft(n);
  std::vector<bool> cand(m), done(m);
  std::vector<std::deque<std::pair<int, int>>> g(n);
  for (int i = 0; i < m; i++) {
    auto a = es[i].second.first, b = es[i].second.second;
    auto y = es[i].first;
    auto xx = w[uft.root(a)] + (uft.same(a, b) ? 0 : w[uft.root(b)]);
    uft.unite(a, b);
    w[uft.root(a)] = xx;
    if (y <= xx) {
      cand[i] = true;
    }
    g[a].emplace_back(b, i);
    g[b].emplace_back(a, i);
  }

  int ret = 0;
  for (int i = m - 1; i >= 0; i--) {
    if (done[i])
      continue;
    if (!cand[i]) {
      ret++;
      continue;
    }
    std::deque<int> q{es[i].second.first, es[i].second.second};
    while (!q.empty()) {
      auto v = q.front();
      q.pop_front();
      for (auto el : g[v]) {
        if (!done[el.second] && es[el.second].first <= es[i].first) {
          q.push_back(el.first);
          done[el.second] = true;
        }
      }
    }
  }

  std::cout << ret << std::endl;

  return 0;
}