_ScreamBrother_ @ 2024-06-29 22:03:11
请大佬帮我看看我的代码哪里出了问题,提交了好几次,一直RE:
代码:
#include <bits/stdc++.h>
struct Edge {
int u, v, w;
bool operator < (Edge __a) { return w < __a.w; }
} G[200010];
int f[10010] = {}, ans, N, M, cnt;
int find(int x) { return x != f[x] ? (f[x] = find(f[x])) : x; }
int m(int x, int y) { f[find(x)] = find(y); }
bool q(int x, int y) { return find(x) == find(y); }
int main() {
std::cin >> N >> M;
for (int i = 1; i <= N; f[i] = i, i ++);
for (int i = 1; i <= M; i ++) std::cin >> G[i].u >> G[i].v >> G[i].w;
std::sort(G + 1, G + M + 1);
for (int i = 1; i <= M; i ++)
if (!q(G[i].u, G[i].v)) {
ans += G[i].w, m(G[i].u, G[i].v), cnt ++;
if (cnt == N - 1) break;
}
ans != 0 ? std::cout << ans : std::cout << "orz";
return 0;
}
by zhouzihang1 @ 2024-06-29 22:11:22
@liyongan041100 m()
的类型要是 void
by tmp_get_zip_diff @ 2024-06-30 17:06:13
@liyongan041100 把 O2 关了
by _ScreamBrother_ @ 2024-07-01 12:59:35
@zhouzihang1 多谢,以AC