1 条题解
-
0
错了三次!本来这种模板题是不想写题解的。
```cpp #include <bits/stdc++.h> #define int long long using namespace std; int T = 1; const int N = 2e5 + 1; int n, m, cnt; int ans; struct Node { int u; int v; int w; } e[N]; int father[N]; void Build() { for (int i = 0; i <= n; i++) { father[i] = i; } } int Find(int u) { if (u != father[u]) father[u] = Find(father[u]); return father[u]; } bool Cmp(const Node& a, Node& b) { return a.w < b.w; } void Solve() { cin >> n >> m; Build(); for (int i = 1; i <= m; i++) { cin >> e[i].u >> e[i].v >> e[i].w; } sort(e + 1, e + 1 + m, Cmp); for (int i = 1; i <= m; i++) { int fu = Find(e[i].u), fv = Find(e[i].v); if (fu != fv) { father[fu] = fv; ans += e[i].w; cnt++; } } if (cnt != n - 1) cout << "orz"; else cout << ans; } signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); while (T--) { Solve(); } return 0; }
- 1
信息
- ID
- 818
- 时间
- 1000ms
- 内存
- 128MiB
- 难度
- 普及
- 标签
- 递交数
- 4
- 已通过
- 1
- 上传者