RE求调玄一关

P3366 【模板】最小生成树

orz
by yudongkai1234 @ 2024-05-07 19:16:40


@[zhangjiayii](/user/939526) ```cpp #include <bits/stdc++.h> using namespace std; #define N 10005 #define M 10000005 struct Edge { int u, v, w; } edge[M]; int s[N]; int find_set(int x) { if (x != s[x]) s[x] = find_set(s[x]); return s[x]; } bool EdgeCmp(Edge a, Edge b) { return a.w < b.w; } int n, m; void kruskal() { sort(edge + 1, edge + 1 + m, EdgeCmp); for (int i = 1; i <= n; i++) s[i] = i; int ans = 0, cnt = 0; for (int i = 1; i <= m; i++) { if (n - 1 == cnt) break; int e1 = find_set(edge[i].u); int e2 = find_set(edge[i].v); if (e1 == e2) continue; else { ans += edge[i].w; s[e1] = e2; cnt++; } } if (n - 1 == cnt) cout << ans; else cout << "orz"; } int main() { cin >> n >> m; for (int i = 1; i <= m; i++) cin >> edge[i].u >> edge[i].v >> edge[i].w; kruskal(); return 0; } ```
by DBL_MAX @ 2024-06-10 15:07:40


|