1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| #include <bits/stdc++.h> using namespace std; const int maxn = 5e3 + 50, maxm = 2e5 + 50; int n, m, ans, cnt, fa[maxn];
struct node { int x_, y_, z_; bool operator<(const node &a) const { return z_ < a.z_; } } e[maxm];
int find(int x) { if (x == fa[x]) return x; return fa[x] = find(fa[x]); }
void kruskal() { sort(e + 1, e + 1 + m); for (int x, y, i = 1; i <= m; ++i) { x = find(e[i].x_), y = find(e[i].y_); if (x != y) { ++cnt, ans += e[i].z_, fa[x] = y; } } }
int main() { cin >> n >> m; for (int i = 1; i <= n; ++i) fa[i] = i; for (int x, y, z, i = 1; i <= m; ++i) { cin >> x >> y >> z; e[i] = {x, y, z}; } kruskal(); if (cnt == n - 1) cout << ans << endl; else cout << "orz" << endl; return 0; }
|