边带权并查集求调(玄关
查看原帖
边带权并查集求调(玄关
1423269
ini_____楼主2024/11/20 21:29

50pts

#include<bits/stdc++.h>
using namespace std;
const int N=1e5;
pair<int,pair<int,int> >edge[N];
int fa[N];
bool dis[N];
int find(int x){
	if(fa[x]==x)return x;
	fa[x]=find(fa[x]);
	dis[x]^=dis[fa[x]];
	return fa[x];
}
void merge(int x,int y,bool d){
	x=find(x),y=find(y);
	fa[y]=x;
	dis[y]=d;
}
int main(){
	int n,m;
	cin>>n>>m;
	for(int i=1;i<=n;i++)fa[i]=i;
	for(int i=0;i<m;i++)cin>>edge[i].second.first>>edge[i].second.second>>edge[i].first;
	sort(edge,edge+m);
	for(int i=m-1;i>=0;i--){
		int u=edge[i].second.second,v=edge[i].second.first;
		if(find(u)==find(v)){
			if(dis[u]==dis[v]){
				cout<<edge[i].first<<endl;
				return 0;
			}
		}
		else merge(u,v,!dis[u]^dis[v]);
	}
	cout<<0;
	return 0;
}
 
2024/11/20 21:29
加载中...