Problem p02364 (C++) — buggy submission, verdict: Time Limit Exceeded

///
// File:  grl_2_a.cpp
// Author: ymiyamoto
//
// Created on Fri Oct 20 22:04:46 2017
//

#include <algorithm>
#include <cstdint>
#include <cstdio>
#include <vector>

using namespace std;
#define SIZE 100

struct node_t {
  node_t *parent;
  uint32_t val;
  node_t(uint32_t val) : parent(this), val(val){};
};

struct edge_t {
  uint32_t from;
  uint32_t to;
  uint32_t weight;
  bool operator<(const edge_t &e) const { return weight < e.weight; }

  void print() { printf("from: %u, to: %u, weight: %u\\n", from, to, weight); }
};

static vector<node_t *> nodes;
static vector<int32_t> height;
static vector<edge_t> edges;

node_t *root(node_t *node) {
  if (node->parent == node) {
    return node;
  } else {
    return root(node->parent);
  }
}

void join(uint32_t a, uint32_t b) {
  node_t *a_root = root(nodes[a]);
  node_t *b_root = root(nodes[b]);
  if (height[a_root->val] < height[b_root->val]) {
    a_root->parent = b_root;
  } else {
    b_root->parent = a_root;
    if (height[a_root->val] == height[b_root->val]) {
      height[b_root->val]++;
    }
  }
}

bool same(uint32_t a, uint32_t b) {
  return root(nodes[a])->val == root(nodes[b])->val;
}

int32_t main() {
  uint32_t V, E;
  scanf("%u %u", &V, &E);

  for (uint32_t i = 0; i < V; i++) {
    node_t *node = new node_t(i);
    nodes.push_back(node);
    height.push_back(1);
  }

  for (uint32_t i = 0; i < E; i++) {
    uint32_t f, t, w;
    scanf("%u %u %u", &f, &t, &w);
    edges.push_back({f, t, w});
  }

  sort(edges.begin(), edges.end());

  uint64_t weight = 0;
  for (uint32_t i = 0; i < edges.size(); i++) {
    edge_t edge = edges[i];
    if (!same(edge.from, edge.to)) {
      join(edge.from, edge.to);
      weight += edge.weight;
    }
  }

  printf("%lu\\n", weight);

  return 0;
}

Error: TLE

Fixed submission:

///
// File:  grl_2_a.cpp
// Author: ymiyamoto
//
// Created on Fri Oct 20 22:04:46 2017
//

#include <algorithm>
#include <cstdint>
#include <cstdio>
#include <vector>

using namespace std;
#define SIZE 100

struct node_t {
  node_t *parent;
  vector<node_t *> children;
  uint32_t val;
  node_t(uint32_t val) : parent(this), val(val){};
};

struct edge_t {
  uint32_t from;
  uint32_t to;
  uint32_t weight;
  bool operator<(const edge_t &e) const { return weight < e.weight; }

  void print() { printf("from: %u, to: %u, weight: %u\\n", from, to, weight); }
};

static vector<node_t *> nodes;
static vector<edge_t> edges;

node_t *root(node_t *node) {
  if (node->parent == node) {
    return node;
  } else {
    return root(node->parent);
  }
}

void join(uint32_t a, uint32_t b) {
  node_t *a_root = root(nodes[a]);
  node_t *b_root = root(nodes[b]);
  a_root->parent = b_root;
  b_root->children.push_back(a_root);
}

bool same(uint32_t a, uint32_t b) {
  return root(nodes[a])->val == root(nodes[b])->val;
}

int32_t main() {
  uint32_t V, E;
  scanf("%u %u", &V, &E);

  for (uint32_t i = 0; i < V; i++) {
    node_t *node = new node_t(i);
    nodes.push_back(node);
  }

  for (uint32_t i = 0; i < E; i++) {
    uint32_t f, t, w;
    scanf("%u %u %u", &f, &t, &w);
    edges.push_back({f, t, w});
    edges.push_back({t, f, w});
  }

  sort(edges.begin(), edges.end());

  uint64_t weight = 0;
  for (uint32_t i = 0; i < edges.size(); i++) {
    edge_t edge = edges[i];
    if (!same(edge.from, edge.to)) {
      join(edge.from, edge.to);
      weight += edge.weight;
    }
  }

  printf("%lu\\n", weight);

  return 0;
}