Need help!!!
查看原帖
Need help!!!
1023102
I_AK_IOI_EVERY_DAY楼主2024/3/1 12:45
#include<bits/stdc++.h>
using namespace std;
int n,m,f[10001],ch,times;
int find(int x){
	if(f[x]==x)return x;
	return f[x]=find(f[x]);
}
struct node{
	int x,y,t;
}a[10001];
bool cmp(node a,node b){
	return a.t<b.t;
}
int check(int x,int y){
	if(f[find(x)]==find(y))return 1;
	return 0;
}
int main(){
	cin>>n>>m;
	for(int i=1;i<=m;i++){
		cin>>a[i].x>>a[i].y>>a[i].t;
		f[i]=i;
	}
	sort(a+1,a+m+1,cmp);
	for(int i=1;i<=m;i++){
		f[find(a[i].x)]=find(a[i].y);
		if(check(a[i].x,a[i].y)==1){
			ch=1;
			times=a[i].t;
		}
		else{
			ch=0;
		}
	}
	if(ch==1){
		cout<<times<<endl;
	}
	else cout<<"-1"<<endl;
	return 0;
}

5WA5RE(悲)

2024/3/1 12:45
加载中...