最小生成树(Kruskal)
前置知识:并查集、贪心、图的存储
从最小边权开始,按边权从小到大依次加入(贪心),利用并查集来维护是否产生了环,如果某次加边产生了环,就不加这条边,直到加入了
【模板】最小生成树
题目描述
如题,给出一个无向图,求出最小生成树,如果该图不连通,则输出 orz。
输入格式
第一行包含两个整数
接下来
输出格式
如果该图连通,则输出一个整数表示最小生成树的各边的长度之和。如果该图不连通则输出 orz。
样例 #1
样例输入 #1
4 5
1 2 2
1 3 2
1 4 3
2 3 4
3 4 3样例输出 #1
7提示
数据规模:
对于
对于
对于
对于
样例解释:

所以最小生成树的总边权为
solve
c++
#include <bits/stdc++.h>
using i64 = long long;
struct Edge {
int u, v, w;
bool operator<(const Edge &t) const {
return w < t.w;
}
};
void solve() {
int n, m;
std::cin >> n >> m;
std::vector<int> fa(n + 1);
std::iota(fa.begin(), fa.end(), 0);
std::vector<Edge> adj(m);
for (int i = 0; i < m; i++) {
int x, y, z;
std::cin >> x >> y >> z;
adj[i] = {x, y, z};
}
std::sort(adj.begin(), adj.end());
auto find = [&](auto &&self, int x) -> int {
return x == fa[x] ? x : fa[x] = self(self, fa[x]);
};
i64 ans = 0, cnt = 0;
for (int i = 0; i < m; i++) {
auto [u, v, w] = adj[i];
int x = find(find, u), y = find(find, v);
if (x != y) {
fa[x] = y;
ans += w;
cnt++;
}
}
if (cnt == n - 1) {
std::cout << ans << "\n";
} else {
std::cout << "orz\n";
}
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int _ = 1;
// cin >> _;
while (_--) {
solve();
}
return 0;
}