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

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

const int N = 1e4;
int n, m, ans;
vector<pair<int, pair<int, int>>> g;
vector<pair<int, int>> MST;
int parent[N];

int root(int a) {
  if (parent[a] == a)
    return a;
  parent[a] = root(parent[a]);
  return root(parent[a]);
}

void Union(int a, int b) { parent[root(a)] = root(b); }

void find_mst() {
  sort(g.begin(), g.end());
  MST.clear();
  for (int i = 0; i < n; i++)
    parent[i] = i;
  for (int i = 0; i < m; i++) {
    int f = g[i].second.first;
    int t = g[i].second.second;
    if (root(f) != root(t)) {
      MST.push_back(g[i].second);
      ans += g[i].first;
      Union(f, t);
    }
  }
}

int main() {
  cin >> n >> m;
  g.clear();
  for (int i = 0; i < m; i++) {
    int f, t, w;
    cin >> f >> t >> w;
    g.push_back(make_pair(w, make_pair(f, t)));
  }
  find_mst();
  cout << ans << endl;
}

Error: 0

Fixed submission:

//*********************************************************************************************************************************
//*
//**
//*		SSSSSSSSSSSS
//MMMM			MMMM	HHHH		HHHH	DDDDDDDDDDDD
//**
//*		SSSSSSSSSSSS	CSAA Numbered
//MMMMM		   MMMMM	HHHH		HHHH	DDDDDDDDDDDDD
//**
//*		SSSS				A PROGRAM BY:
//MMMMMM		  MMMMMM	HHHH		HHHH	DDDD	 DDDDD
//**
//*		SSSS
//MMMMMMM		 MMMMMMM	HHHH		HHHH	DDDD	   DDDD
//** *		SSSSSSSSSSSS	AAAAAAAAAAAA	MMMMMMMMMMMMMMMMMMMM	MMMMMMMM
//MMMMMMMM	HHHHHHHHHHHHHHHH	DDDD	    DDD		** *
//SSSSSSSSSSSS	AAAAAAAAAAAA	MMMMMMMMMMMMMMMMMMMM	MMMM MMMM  MMMM MMMM
//HHHHHHHHHHHHHHHH	DDDD	    DDD		**
//*				SSSS	AAAA	AAAA	MMMM	MMMM
//MMMM	MMMM  MMMMMMMM	MMMM	HHHH		HHHH	DDDD DDDD
//**
//*				SSSS	AAAA	AAAA	MMMM	MMMM
//MMMM	MMMM   MMMMMM	MMMM	HHHH		HHHH	DDDD DDDD
//**
//*		SSSSSSSSSSSS	AAAAAAAAAAAAA	MMMM	MMMM	MMMM MMMM MMMM
//MMMM	HHHH		HHHH	DDDDDDDDDDDDD		** *
//SSSSSSSSSSSS	AAAAAAAAAAAAAA	MMMM	MMMM	MMMM	MMMM
//MMMM	HHHH		HHHH	DDDDDDDDDDDD		** *
//**
//********************************************************************************************************************************
//|| ||
//||	CSAA AA###B ddmmYYYY					||
//||	? SMP(SAM-MHD-PGM) @ AEH_COT96			||
//||	Program Name:							||
//||			AA###B_<xxxxNAMExxxx>			||
//|| ||
/////******************************************\\\\

#include <iostream>
#include <algorithm>
#include <vector>

typedef std::pair<int, int> pii;
using namespace std;
const int maxn = 3e3 + 3;
vector<pair<int, pii>> Graph;
vector<pair<int, pii>> MST;
int n, m, parent[maxn];

#define _va .second.first
#define _vb .second.second
#define _w .first

int getRoot(int d) {
  if (parent[d] == d)
    return d;
  parent[d] = getRoot(parent[d]);
  return getRoot(parent[d]);
}

void MergeComp(int dx, int dy) { parent[getRoot(dx)] = getRoot(dy); }

bool isSameComp(int dx, int dy) { return (getRoot(dx) == getRoot(dy)); }

void build_MST() {
  sort(Graph.begin(), Graph.end());
  MST.clear();
  for (int i = 0; i < n; i++)
    parent[i] = i;
  for (int i = 0; i < m; i++) {
    int v = Graph[i] _va;
    int u = Graph[i] _vb;
    int w = Graph[i] _w;
    if (!isSameComp(v, u)) {
      MST.push_back(make_pair(w, make_pair(v, u)));
      MergeComp(v, u);
    }
  }
}

int main() {

  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  cin >> n >> m;
  Graph.clear();
  for (int i = 0; i < m; i++) {
    int v, u, w;
    cin >> v >> u >> w;
    Graph.push_back(make_pair(w, make_pair(v, u)));
  }

  build_MST();

  int ans = 0;
  for (pair<int, pii> gData : MST)
    ans += gData.first;

  cout << ans << endl;
}
/**/

// ? SMP(SAM-MHD-PGM) @ AEH_COT96
//   SamMHD (Saman Mahdanian)