怎么MLE的(悬一关
查看原帖
怎么MLE的(悬一关
773747
yutong_Seafloor楼主2023/9/12 11:06

并查集:

#include<bits/stdc++.h>
using namespace std;
int f[100010],fx,fy,ans;
struct node
{
	int x,y,t;
}yt[100010];
bool px(node c,node d)
{
	return c.t>d.t;
}
int find(int x)
{
	if(f[x]==x) return f[x]==find(f[x]);
	else return x;
}
int n,m,i;
int main()
{
	cin>>n>>m;
	for(i=1;i<=m;i++)
	cin>>yt[i].x>>yt[i].y>>yt[i].t;
	sort(yt+1,yt+m+1,px);
	for(i=1;i<=n;i++)
	f[i]=i;
	for(i=1;i<=m;i++)
	{
		fx=find(yt[i].x);
		fy=find(yt[i].y);
		if(fx!=fy)
		{
			f[fx]=fy;
			n--;
			ans=yt[i].t;
		}
	}
	if(n-1==1) cout<<ans;
	else cout<<"-1";
}

然而内存炸了

MLE

2023/9/12 11:06
加载中...