最后一个测试点错了,急84分!!!
查看原帖
最后一个测试点错了,急84分!!!
959414
mjyhhhhh楼主2023/9/14 20:34
#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;
}

测试点 !!!!!!!!!!!!!!!!!!!

2023/9/14 20:34
加载中...