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

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>

#define LL long long
#define VI vector<int>
#define VL vector<long long>
#define FOR(i, a, b) for (int i = (a); i < ((int)b); ++i)
#define RFOR(i, a) for (int i = (a); i >= 0; --i)
#define FOE(i, a) for (auto i : a)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define DUMP(x) cerr << #x << " = " << (x) << endl;
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v, x) (std::find(v.begin(), v.end(), x) != v.end())
#define BIT(n) (1LL << (n))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
#define EPS 1e-14

using namespace std;

struct Edge {
  int from;
  int to;
  int cost;
  Edge(int from, int to, int cost) : from(from), to(to), cost(cost) {}
  bool operator<(const Edge &edge) const { return this->cost < edge.cost; }
  bool operator>(const Edge &edge) const { return this->cost > edge.cost; }
};

class UnionFind {
public:
  vector<int> parent;
  vector<int> setSize;

  UnionFind(unsigned int size) {
    parent.resize(size + 1);
    iota(parent.begin(), parent.end(), 0);
    setSize.resize(size + 1, 1);
  }

  // x??¨y???????????????????±??????????
  bool isSameSet(int x, int y) { return findRoot(x) == findRoot(y); }

  // x??¨y????±??????????????????????
  void unionSet(int x, int y) {
    x = findRoot(x);
    y = findRoot(y);
    if (x == y) {
      return;
    }

    parent[x] = y;
    setSize[y] += setSize[x];
  }

private:
  // ??¨??????????±???????
  int findRoot(int x) {
    if (parent[x] == x) {
      return x;
    } else {
      return parent[x] = findRoot(parent[x]);
    }
  }
};

/**
 * ??°?????????????°???¨?????¨????±??????????O(|E| log|V|)
 * @param num_of_node ???????????°
 * @param edges?????¨?????????????????°????????????u -> v??¨v ->
 * u??????????????\\??????
 * @return
 */
long long kruskal(unsigned int num_of_node, vector<Edge> edges) {
  sort(edges.begin(), edges.end());

  UnionFind uf(num_of_node);

  long long ans = 0;
  for (Edge e : edges) {
    if (!uf.isSameSet(e.from, e.to)) {
      uf.unionSet(e.from, e.to);
      ans += e.cost;
    }
  }
  return ans;
}

int main(int argc, char *argv[]) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int V, E;
  cin >> V >> E;

  vector<Edge> edges;
  FOR(i, 0, E) {
    int s, t, w;
    cin >> s >> t >> w;
    edges.emplace_back(Edge(s, t, w));
    edges.emplace_back(Edge(t, s, w));
  }

  cout << kruskal(V, edges) << endl;

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>

#define LL long long
#define VI vector<int>
#define VL vector<long long>
#define FOR(i, a, b) for (int i = (a); i < ((int)b); ++i)
#define RFOR(i, a) for (int i = (a); i >= 0; --i)
#define FOE(i, a) for (auto i : a)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define DUMP(x) cerr << #x << " = " << (x) << endl;
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v, x) (std::find(v.begin(), v.end(), x) != v.end())
#define BIT(n) (1LL << (n))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
#define EPS 1e-14

using namespace std;

/**
 *
 * @param num_of_node ???????????°
 * @param cost cost[u][v]??????e=(u,v)????????????(?????¨???????????´??????INF)
 * @return
 */
long long prim(unsigned int num_of_node, vector<vector<int>> cost) {
  vector<int> min_cost(num_of_node,
                       INT_MAX); // ??????X???????????????????°??????????
  vector<bool> used(num_of_node, false); // ??????i???X????????????????????????

  min_cost[0] = 0;

  long long ans = 0;
  while (true) {
    // X????±?????????????????????????X???????????????????????????????°?????????????????????¢???
    int v = -1;
    for (int u = 0; u < num_of_node; ++u) {
      if (!used[u] && (v == -1 || min_cost[u] < min_cost[v])) {
        v = u;
      }
    }

    if (v == -1) {
      break;
    }
    used[v] = true;
    ans += min_cost[v];
    for (int u = 0; u < num_of_node; ++u) {
      min_cost[u] = min(min_cost[u], cost[v][u]);
    }
  }

  return ans;
}

int main(int argc, char *argv[]) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int V, E;
  cin >> V >> E;

  vector<vector<int>> cost(V, vector<int>(V, INT_MAX));
  FOR(i, 0, E) {
    int s, t, w;
    cin >> s >> t >> w;
    cost[s][t] = w;
    cost[t][s] = w;
  }

  cout << prim(V, cost) << endl;

  return 0;
}