为什么错了
  • 板块P1111 修复公路
  • 楼主noruto
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/7/10 16:36
  • 上次更新2024/7/10 17:32:51
查看原帖
为什么错了
965557
noruto楼主2024/7/10 16:36
#include<bits/stdc++.h>
using namespace std;
const int N=1e5;
int n,m,a[N];
struct node{
	int x;
	int y;
	int t;
}s[50001];
int find(int x){
	if(x!=a[x])a[x]=find(a[x]);
	return a[x];
}
void hb(int x,int y){
	x=find(x),y=find(y);
	if(x!=y)a[x]=a[y];
}
bool cmp(node x,node y){
	x.t<y.t;
}bool cal(){
	for(int i=1;i<=n-1;i++)if(find(i)!=find(i+1))return 0;
	return 1;	
}
int main(){
	cin>>n>>m;
	for(int i=1;i<=m;i++)cin>>s[i].x>>s[i].y>>s[i].t;
	for(int i=1;i<=n;i++)a[i]=i;	
	sort(s+1,s+1+m,cmp);
	for(int i=1;i<=m;i++){
		hb(s[i].x,s[i].y);
		if(cal()){
			cout<<s[i].t;
			return 0;
		}
	}cout<<-1;	
	return 0;
}
2024/7/10 16:36
加载中...