mjyhhhhh @ 2023-09-14 20:34:51
#include<bits/stdc++.h>
using namespace std;
struct node {
int x, y, c;
} a[500010];
int n, m, f[500010], ans, t;
int g(int x) {
if (f[x] == x) return x;
else {
f[x] = g(f[x]);
return f[x];
}
}
bool cmp(node a, node b) {
return a.c < b.c;
}
int main() {
cin>>n>>m;
for (int i = 1; i <= n; i++) f[i] = i;
for (int i = 1; i <= m; i++){
cin>>a[i].x>>a[i].y>>a[i].c;
}
sort(a + 1, a + m + 1, cmp);
for (int i = 1; i <= m; i++) {
int fx, fy;
fx = g(a[i].x);
fy = g(a[i].y);
if (fx != fy) {
ans += a[i].c;
t++;
f[fy] = fx;
if (t == n - 1) break;
}
}
cout<<ans;
return 0;
}
测试点 !!!!!!!!!!!!!!!!!!!
by xuchuhan @ 2023-09-14 20:39:55
@mjyhhhhh
如果该图不连通,则输出
orz
。
by mjyhhhhh @ 2023-09-14 20:41:30
@xuchuhan 谢谢,刚发就有人解答了,太感谢了
by mjyhhhhh @ 2023-09-14 20:42:52
@Misty_Hazers 我测试点的图片链接啊?
by GoodLuckCat @ 2023-09-14 20:43:12
@Misty_Hazers https://www.luogu.com.cn/record/124706684
by mjyhhhhh @ 2023-09-14 20:45:47
这么多人做这题吗
by mjyhhhhh @ 2023-09-14 20:55:52
@xuchuhan 过了,谢谢