Problem p03143 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <iostream>
#include <map>
#include <random>
#include <set>
#include <time.h>
#include <unordered_map>
#include <vector>

#define hash akasd
#define prev aaaasds
#define rank asdfdsf

using namespace std;

typedef long long ll;
typedef long double dbl;

template <class T> void print(vector<T> s) {
  for (T i : s)
    cout << i << " ";
  cout << endl;
}

const int maxn = 1e5 + 7;

int prev[maxn], rank[maxn];

int getPrev(int v) {
  if (v == prev[v])
    return v;
  prev[v] = getPrev(prev[v]);
  return prev[v];
}

ll summa[maxn];

void join(int a, int b) {
  a = getPrev(a);
  b = getPrev(b);
  if (a == b)
    return;
  if (rank[a] < rank[b])
    swap(a, b);
  prev[b] = a;
  summa[a] += summa[b];
  if (rank[a] == rank[b])
    rank[a]++;
}

ll kek[maxn];

vector<pair<int, int>> g[maxn];
bool ok[maxn];

void dfs(int v, ll x) {
  ok[v] = 1;
  for (pair<int, int> u : g[v]) {
    if (!ok[u.second] && u.first <= x)
      dfs(u.second, x);
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  freopen("test.in", "r", stdin);
  int n, m;
  cin >> n >> m;
  vector<int> s(n);
  for (int i = 0; i < n; i++)
    cin >> s[i];
  vector<pair<int, pair<int, int>>> lst(m);
  for (int i = 0; i < m; i++) {
    int a, b, x;
    cin >> a >> b >> x;
    lst[i] = {x, {a - 1, b - 1}};
    g[a - 1].push_back({x, b - 1});
    g[b - 1].push_back({x, a - 1});
  }
  sort(lst.begin(), lst.end());
  for (int i = 0; i < n; i++) {
    prev[i] = i;
    summa[i] = s[i];
  }
  for (int i = 0; i < m; i++) {
    join(lst[i].second.first, lst[i].second.second);
    kek[i] = summa[getPrev(lst[i].second.first)];
  }
  // for (int i = 0; i < m; i++)
  //     cout << kek[i] << " ";
  // cout << endl;
  int answer = 0;
  for (int i = m - 1; i >= 0; i--) {
    int a = lst[i].second.first, b = lst[i].second.second;
    if (ok[a] && ok[b]) {
      answer++;
      continue;
    }
    if (lst[i].first <= kek[i]) {
      answer++;
      dfs(a, kek[i]);
    }
  }
  cout << m - answer << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <iostream>
#include <map>
#include <random>
#include <set>
#include <time.h>
#include <unordered_map>
#include <vector>

#define hash akasd
#define prev aaaasds
#define rank asdfdsf

using namespace std;

typedef long long ll;
typedef long double dbl;

template <class T> void print(vector<T> s) {
  for (T i : s)
    cout << i << " ";
  cout << endl;
}

const int maxn = 1e5 + 7;

int prev[maxn], rank[maxn];

int getPrev(int v) {
  if (v == prev[v])
    return v;
  prev[v] = getPrev(prev[v]);
  return prev[v];
}

ll summa[maxn];

void join(int a, int b) {
  a = getPrev(a);
  b = getPrev(b);
  if (a == b)
    return;
  if (rank[a] < rank[b])
    swap(a, b);
  prev[b] = a;
  summa[a] += summa[b];
  if (rank[a] == rank[b])
    rank[a]++;
}

ll kek[maxn];

vector<pair<int, int>> g[maxn];
bool ok[maxn];

void dfs(int v, ll x) {
  ok[v] = 1;
  for (pair<int, int> u : g[v]) {
    if (!ok[u.second] && u.first <= x)
      dfs(u.second, x);
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  // freopen("test.in", "r", stdin);
  int n, m;
  cin >> n >> m;
  vector<int> s(n);
  for (int i = 0; i < n; i++)
    cin >> s[i];
  vector<pair<int, pair<int, int>>> lst(m);
  for (int i = 0; i < m; i++) {
    int a, b, x;
    cin >> a >> b >> x;
    lst[i] = {x, {a - 1, b - 1}};
    g[a - 1].push_back({x, b - 1});
    g[b - 1].push_back({x, a - 1});
  }
  sort(lst.begin(), lst.end());
  for (int i = 0; i < n; i++) {
    prev[i] = i;
    summa[i] = s[i];
  }
  for (int i = 0; i < m; i++) {
    join(lst[i].second.first, lst[i].second.second);
    kek[i] = summa[getPrev(lst[i].second.first)];
  }
  // for (int i = 0; i < m; i++)
  //     cout << kek[i] << " ";
  // cout << endl;
  int answer = 0;
  for (int i = m - 1; i >= 0; i--) {
    int a = lst[i].second.first, b = lst[i].second.second;
    if (ok[a] && ok[b]) {
      answer++;
      continue;
    }
    if (lst[i].first <= kek[i]) {
      answer++;
      dfs(a, kek[i]);
    }
  }
  cout << m - answer << endl;
  return 0;
}