30分求助大佬
查看原帖
30分求助大佬
138799
jzdywf楼主2021/11/6 16:53
#include<bits/stdc++.h>
using namespace std;
int mid;
int cnt;
int ans;
int u,v,w;
int n,m,l,r;
struct edge{
	int w;
	int to;
	int nxt;
}e[210000];
int head[21000];
int used[21000];
int color[21000];
void add(int x,int y,int z){
	cnt++;
	e[cnt].w=z;
	e[cnt].to=y;
	e[cnt].nxt=head[x];
	head[x]=cnt;
	return;
}
bool dfs(int now,int x,int Color){
	if(used[now]){
		if(color[now]==Color){
			return true;
		}
		return false;
	}
	used[now]=1;
	color[now]=Color;
	for(int i=head[now];i;i=e[i].nxt){
		if(e[i].w<=x){
			continue;
		}
		if(!dfs(e[i].to,x,(Color+1)%2)){
			return false;
		}
	}
	return true;
}
int main(){
	cin>>n>>m;
	for(int i=1;i<=m;i++){
		cin>>u>>v>>w;
		r=max(r,w);
		add(u,v,w);
		add(v,u,w);
	}
	while(l<=r){
		mid=(l+r)>>1;
		if(dfs(1,mid,0)){
			r=mid-1;
			ans=mid;
		}
		else{
			l=mid+1;
		}
		memset(used,0,sizeof(used));
		memset(color,0,sizeof(color));
	}
	cout<<ans;
	return 0;
}

二分图+二分不知道为什么WA了七个点

2021/11/6 16:53
加载中...