样例过零分求助qwq
查看原帖
样例过零分求助qwq
552610
__Shine__楼主2023/7/15 14:30

kruskal

#include<bits/stdc++.h>
using namespace std;
int f[20000010];
int n,m,k,cnt,ans;
struct e {
	int u;
	int v;
	int w;
} edge[20000010];
int Find(int x) {
	if(f[x]==x)return x;
	else return f[x]=Find(f[x]);
}
void init() {
	for(int i=1; i<=n; i++)
		f[i]=i;
}
void Merge(int x,int y)
{
	int fx=Find(x),fy=Find(y);
	if(fx!=fy)
		f[fx]=fy;
}
bool cmp(e a,e b) {
	return a.w<b.w;
}
int main() {

	cin>>n>>m;
	init();
	for(int i=1; i<=m; i++) 
		cin>>edge[i].u>>edge[i].v>>edge[i].w;
	for(int i=1; i<=m; i++) 
		if(Find(edge[i].u)!=Find(edge[i].v)) {
			ans=max(edge[i].w,ans);
			cnt++;
			Merge(edge[i].u,edge[i].v);
			if(cnt==n-1)break;
		}
	cout<<n-1<<" "<<ans;
	return 0;
}
2023/7/15 14:30
加载中...