Problem p03143 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long // aaaaaaaaaaaaaaaaaa
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define allr(x) (x).rbegin(), (x).rend()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define SIZE(buff) (sizeof(buff) / sizeof(buff[0]))
typedef vector<int> VI;
typedef vector<vector<int>> VVI;
typedef long long ll;
typedef pair<int, int> PII;
typedef vector<pair<int, int>> VPII;
const ll mod = 1000000007;
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
void chmin(int64_t &a, int64_t b) { a = min(a, b); }
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "{";
  for (int i = 0; i < (int)v.size(); i++)
    o << (i > 0 ? ", " : "") << v[i];
  o << "}";
  return o;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, pair<T, U> &pair_var) {
  os << "(" << pair_var.first << ", " << pair_var.second << ")";
  return os;
}
// head
int q, m, n, k, res, tmp, tmp2, tmp3, h, w;
int a[100001] = {};
// int b[100010] = {};
deque<PII> que;
string s;
map<int, VPII> kukan;
vector<pair<int, PII>> g;
int dp[200000];
int flag;
int ok[100010] = {};
VPII g2[200000];

class UnionFind {
private:
  int n;
  vector<int> uni;

public:
  UnionFind(int _n) {
    n = _n;
    uni.clear();
    uni.resize(n, -1);
    rep(i, 0, n) { uni[i] = -a[i]; }
  }
  int root(int x) {
    if (uni[x] < 0)
      return x;
    return uni[x] = root(uni[x]);
  }
  bool same(int x, int y) { return root(x) == root(y); }
  void unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return;
    if (uni[x] > uni[y])
      swap(x, y);
    uni[x] += uni[y];
    uni[y] = x;
  }
  int getSize(int x) { return -uni[root(x)]; }
  void print() {
    for (auto x : uni)
      cout << x << " ";
    cout << endl;
  }
};

void dfs(int x, int c) {
  rep(j, 0, g2[x].size()) {
    PII i = g2[x][j];
    if (i.se <= c) {
      g2[x][j].se = 1LL << 60;
      res += 1;
      dfs(i.fi, c);
    }
  }
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n >> m;
  rep(i, 0, n) { cin >> a[i]; }
  rep(i, 0, m) {
    cin >> tmp >> tmp2 >> tmp3;
    g.pb({tmp3, {tmp2 - 1, tmp - 1}});
    g2[tmp2 - 1].push_back({tmp - 1, tmp3});
    g2[tmp - 1].push_back({tmp2 - 1, tmp3});
  }
  UnionFind UF(n);
  sort(all(g));
  VI v;
  rep(i, 0, n) {
    pair<int, PII> hen = g[i];
    UF.unite(hen.se.fi, hen.se.se);
    if (UF.getSize(hen.se.fi) >= hen.fi) {
      v.pb(i);
    }
  }
  res = 0;
  reverse(all(v));
  for (int k : v) {
    pair<int, PII> hen = g[k];
    dfs(hen.se.se, hen.fi);
  }
  cout << m - res / 2 << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long // aaaaaaaaaaaaaaaaaa
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define allr(x) (x).rbegin(), (x).rend()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define SIZE(buff) (sizeof(buff) / sizeof(buff[0]))
typedef vector<int> VI;
typedef vector<vector<int>> VVI;
typedef long long ll;
typedef pair<int, int> PII;
typedef vector<pair<int, int>> VPII;
const ll mod = 1000000007;
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
void chmin(int64_t &a, int64_t b) { a = min(a, b); }
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "{";
  for (int i = 0; i < (int)v.size(); i++)
    o << (i > 0 ? ", " : "") << v[i];
  o << "}";
  return o;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, pair<T, U> &pair_var) {
  os << "(" << pair_var.first << ", " << pair_var.second << ")";
  return os;
}
// head
int q, m, n, k, res, tmp, tmp2, tmp3, h, w;
int a[100001] = {};
// int b[100010] = {};
deque<PII> que;
string s;
map<int, VPII> kukan;
vector<pair<int, PII>> g;
int dp[200000];
int flag;
int ok[100010] = {};
VPII g2[200000];

class UnionFind {
private:
  int n;
  vector<int> uni;

public:
  UnionFind(int _n) {
    n = _n;
    uni.clear();
    uni.resize(n, -1);
    rep(i, 0, n) { uni[i] = -a[i]; }
  }
  int root(int x) {
    if (uni[x] < 0)
      return x;
    return uni[x] = root(uni[x]);
  }
  bool same(int x, int y) { return root(x) == root(y); }
  void unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return;
    if (uni[x] > uni[y])
      swap(x, y);
    uni[x] += uni[y];
    uni[y] = x;
  }
  int getSize(int x) { return -uni[root(x)]; }
  void print() {
    for (auto x : uni)
      cout << x << " ";
    cout << endl;
  }
};

void dfs(int x, int c) {
  rep(j, 0, g2[x].size()) {
    PII i = g2[x][j];
    if (i.se <= c) {
      g2[x][j].se = 1LL << 60;
      res += 1;
      dfs(i.fi, c);
    }
  }
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n >> m;
  rep(i, 0, n) { cin >> a[i]; }
  rep(i, 0, m) {
    cin >> tmp >> tmp2 >> tmp3;
    g.pb({tmp3, {tmp2 - 1, tmp - 1}});
    g2[tmp2 - 1].push_back({tmp - 1, tmp3});
    g2[tmp - 1].push_back({tmp2 - 1, tmp3});
  }
  UnionFind UF(n);
  sort(all(g));
  VI v;
  rep(i, 0, m) {
    pair<int, PII> hen = g[i];
    UF.unite(hen.se.fi, hen.se.se);
    if (UF.getSize(hen.se.fi) >= hen.fi) {
      v.pb(i);
    }
  }
  res = 0;
  reverse(all(v));
  for (int k : v) {
    pair<int, PII> hen = g[k];
    dfs(hen.se.se, hen.fi);
  }
  cout << m - res / 2 << endl;
}