有人能帮我看看哪里不对吗?
查看原帖
有人能帮我看看哪里不对吗?
57755
Happy_Orca楼主2022/7/8 16:05

只过了第一个点

#include<bits/stdc++.h>
using namespace std;
int n,m,tot,nxt[200005],dis[200005],son[200005],lnk[20005],q[20005];
int color[20005];
inline int read(){
	int ret=0;char ch=getchar();
	while(!isdigit(ch)) ch=getchar();
	while(isdigit(ch)) ret=ret*10+ch-'0',ch=getchar();
	return ret;
}
void add_e(int x,int y,int w){
	son[++tot]=y,dis[tot]=w,nxt[tot]=lnk[x],lnk[x]=tot;
}
bool check(int x){
	memset(color,0,sizeof color);
	for(int i=1;i<=n;i++){
		if(color[i]!=0) continue;
		int t=1,h=0;q[1]=i;color[i]=1;
		while(h<=t){
			h++;
			for(int j=lnk[q[h]];j;j=nxt[j]){
				if(dis[j]<=x) continue;
				if(color[son[j]]==0) q[++t]=son[j],color[son[j]]=(color[q[h]]%2)+1;
				else if(color[son[j]]==color[q[h]]) return 0;
			}
		}
	}
	return 1;
}
int main(){
	n=read(),m=read();
	int L=0,R=0;
	for(int i=1;i<=m;i++){
		int x=read(),y=read(),z=read();
		add_e(x,y,z);add_e(y,x,z);R=max(R,z);
	}R++;
	while(L<R){
		int mid=(L+R)>>1;
		if(check(mid)) R=mid;
		else L=mid+1;
	}
	printf("%d\n",L);
	return 0;
}
2022/7/8 16:05
加载中...