leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
MST_vector.cpp (390B)
0 // Kruskal's Algorithm
1 // Require UnionFind
3 int get_mst(int n, const vector<edge> &edges) {
4 int weight = 0;
6 UnionFind uf(n);
7 for (int i = 0; i < edges.size() && uf.count() != 1; i++) {
8 const auto &e = edges[i];
9 if (uf.connected(e[0], e[1])) continue;
10 uf.join(e[0], e[1]);
11 weight += e[2];
12 }
14 return uf.count() == 1 ? weight : 1e9 + 7;
15 }