Problem p03143 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <functional>
#include <iostream>
#include <numeric>
#include <tuple>
#include <vector>

int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);
  int n, m;
  std::cin >> n >> m;
  std::vector<int> x(n);
  for (int i = 0; i < n; ++i)
    std::cin >> x[i];
  std::vector<std::tuple<int, int, int>> ed(m);
  for (int i = 0; i < m; ++i) {
    std::cin >> std::get<1>(ed[i]) >> std::get<2>(ed[i]) >> std::get<0>(ed[i]);
    std::get<1>(ed[i])--;
    std::get<2>(ed[i])--;
  }
  std::sort(ed.begin(), ed.end());
  std::vector<int> uf(n), uf2(n);
  std::vector<int64_t> sum(x.begin(), x.end());
  std::vector<std::vector<std::pair<int, int>>> mg(n);
  std::iota(uf.begin(), uf.end(), 0);
  std::iota(uf2.begin(), uf2.end(), 0);

  std::function<int(int, std::vector<int> &)> Find = [&](int x,
                                                         std::vector<int> &uf) {
    if (x == uf[x])
      return x;
    return uf[x] = Find(uf[x], uf);
  };

  std::function<int(int, int, std::pair<int, int>)> Merge =
      [&](int x, int y, std::pair<int, int> e) {
        assert((x = Find(x, uf)) != (y = Find(y, uf)));
        if (mg[x].size() > mg[y].size())
          std::swap(x, y);
        uf[x] = y;
        sum[y] += sum[x];
        mg[y].push_back(e);
        mg[y].insert(mg[y].end(), mg[x].begin(), mg[x].end());
        return y;
      };

  std::vector<std::tuple<int, int, int>> others;
  for (auto &e : ed) {
    int w, u, v;
    std::tie(w, u, v) = e;
    if (Find(u, uf) == Find(v, uf))
      continue;
    u = Merge(u, v, std::make_pair(u, v));
    if (sum[u] >= w) {
      for (auto e : mg[u]) {
        uf2[Find(e.first, uf2)] = Find(e.second, uf2);
      }
    }
  }
  std::vector<int64_t> sum2(n);
  for (int i = 0; i < n; ++i)
    sum2[Find(i, uf2)] += x[i];
  int ans = 0;
  for (auto &e : ed) {
    int w, u, v;
    std::tie(w, u, v) = e;
    if (Find(u, uf2) == Find(v, uf2)) {
      if (sum2[Find(u, uf2)] >= w)
        ++ans;
    }
  }
  std::cout << m - ans << "\n";
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <functional>
#include <iostream>
#include <numeric>
#include <tuple>
#include <vector>

int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);
  int n, m;
  std::cin >> n >> m;
  std::vector<int> x(n);
  for (int i = 0; i < n; ++i)
    std::cin >> x[i];
  std::vector<std::tuple<int, int, int>> ed(m);
  for (int i = 0; i < m; ++i) {
    std::cin >> std::get<1>(ed[i]) >> std::get<2>(ed[i]) >> std::get<0>(ed[i]);
    std::get<1>(ed[i])--;
    std::get<2>(ed[i])--;
  }
  std::sort(ed.begin(), ed.end());
  std::vector<int> uf(n), uf2(n);
  std::vector<int64_t> sum(x.begin(), x.end());
  std::vector<std::vector<std::pair<int, int>>> mg(n);
  std::iota(uf.begin(), uf.end(), 0);
  std::iota(uf2.begin(), uf2.end(), 0);

  std::function<int(int, std::vector<int> &)> Find = [&](int x,
                                                         std::vector<int> &uf) {
    if (x == uf[x])
      return x;
    return uf[x] = Find(uf[x], uf);
  };

  std::function<int(int, int, std::pair<int, int>)> Merge =
      [&](int x, int y, std::pair<int, int> e) {
        assert((x = Find(x, uf)) != (y = Find(y, uf)));
        if (mg[x].size() > mg[y].size())
          std::swap(x, y);
        uf[x] = y;
        sum[y] += sum[x];
        mg[y].push_back(e);
        mg[y].insert(mg[y].end(), mg[x].begin(), mg[x].end());
        return y;
      };

  std::vector<std::tuple<int, int, int>> others;
  for (auto &e : ed) {
    int w, u, v;
    std::tie(w, u, v) = e;
    if (Find(u, uf) == Find(v, uf))
      continue;
    u = Merge(u, v, std::make_pair(u, v));
    if (sum[u] >= w) {
      for (auto e : mg[u]) {
        uf2[Find(e.first, uf2)] = Find(e.second, uf2);
      }
      mg[u].clear();
    }
  }
  std::vector<int64_t> sum2(n);
  for (int i = 0; i < n; ++i)
    sum2[Find(i, uf2)] += x[i];
  int ans = 0;
  for (auto &e : ed) {
    int w, u, v;
    std::tie(w, u, v) = e;
    if (Find(u, uf2) == Find(v, uf2)) {
      if (sum2[Find(u, uf2)] >= w)
        ++ans;
    }
  }
  std::cout << m - ans << "\n";
  return 0;
}