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

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

class Edge {
public:
  int w;
  int u;
  int v;
  Edge() {}
  Edge(int w, int u, int v) : w(w), u(u), v(v) {}
  bool operator<(const Edge &rhs) const { return w < rhs.w; }
};

class UnionFind {
public:
  vector<int> par;
  vector<int> dep;
  UnionFind() {}
  UnionFind(int n) : par(n), dep(n, 1) {
    for (auto i = 0; i < n; ++i)
      par[i] = i;
  }
  int findSet(int x);
  void unite(int x, int y);
  bool same(int x, int y);
};

int UnionFind::findSet(int x) {
  if (par[x] != x)
    par[x] = findSet(par[x]);

  return par[x];
}

void UnionFind::unite(int x, int y) {
  x = findSet(x);
  y = findSet(y);

  if (x == y)
    return;

  if (dep[x] < dep[y]) {
    par[x] = y;
    dep[y] += dep[x];
  } else {
    par[y] = x;
    dep[x] += dep[y];
  }
}

bool UnionFind::same(int x, int y) { return findSet(x) == findSet(y); }

int main() {
  int nv, ne;
  cin >> nv >> ne;

  vector<Edge> edg(ne);

  for (auto i = 0; i < ne; ++i) {
    int u, v, w;
    cin >> u >> v >> w;
    edg[i].u = u;
    edg[i].v = v;
    edg[i].w = w;
  }

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

  UnionFind uf(nv);

  int mintw = 0;

  for (auto i = 0; i < ne; ++i) {
    Edge e = edg[i];

    if (!uf.same(e.u, e.v)) {
      mintw += e.w;
      uf.unite(e.u, e.v);
    }
  }

  cout << mintw << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

class MSTree {
public:
  int nv;
  vector<vector<pair<int, int>>> adj;
  vector<bool> visited;
  MSTree() {}
  MSTree(int n) : nv(n) {
    adj.resize(n);
    visited.resize(n, false);
  }
  void setAdj(int u, int v, int w);
  int prim();
};

void MSTree::setAdj(int u, int v, int w) {
  adj[u].push_back(make_pair(v, w));
  adj[v].push_back(make_pair(u, w));
}

int MSTree::prim() {
  if (nv == 1)
    return 0;

  visited[0] = true;

  int l = 0;

  bool fin = false;

  while (!fin) {
    fin = true;
    int mw = 2000000000;
    int mv = 0;

    for (auto i = 0; i < nv; ++i) {
      if (!visited[i]) {
        fin = false;
        for (auto it = adj[i].begin(); it != adj[i].end(); ++it) {
          int v = (*it).first;
          int w = (*it).second;
          if (visited[v] && mw > w) {
            mw = w;
            mv = i;
          }
        }
      }
    }

    if (!fin) {
      l += mw;
      visited[mv] = true;
    }
  }

  return l;
}

int main() {
  int nv, ne;
  cin >> nv >> ne;

  MSTree m(nv);

  for (int i = 0; i < ne; ++i) {
    int u, v, w;
    cin >> u >> v >> w;
    m.setAdj(u, v, w);
  }

  cout << m.prim() << endl;

  return 0;
}