社区讨论

求条必关

P1525[NOIP 2010 提高组] 关押罪犯参与者 2已保存回复 2

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
2 条
当前快照
1 份
快照标识符
@mm7huizq
此快照首次捕获于
2026/03/01 16:34
7 天前
此快照最后确认于
2026/03/04 11:15
4 天前
查看原帖
CPP
#include <bits/stdc++.h>
using namespace std;
const int N=20010;
const int M=100010;
int n,m,col[N];
struct gragh{
	int tot,hd[N],nxt[M<<1],to[M<<1];
	void add(int a,int b){
		tot++;
		nxt[tot]=hd[a];
		hd[a]=tot;
		to[tot]=b;
	}
}g;
struct edge{
    int a,b,c;
}edges[M];
bool cmp(edge a,edge b){
	return a.c<b.c;
}
bool dfs(int u,int fa,int color){
	col[u]=color;
	for(int i=g.hd[u];i;i=g.nxt[i]){
		int v=g.to[i];
		//cout<<u<<" "<<v<<endl;
		if(v==fa)continue;
		if(col[v]==color){
			return 0;
		}
		if(col[v]==0&&!dfs(v,u,3-color)){
			return 0;
		}
	}
	return 1;
}
bool check(int p){
	g.tot=0;
	memset(g.hd,0,sizeof(g.hd));
	memset(g.nxt,0,sizeof(g.nxt));
	memset(g.to,0,sizeof(g.to));
	memset(col,0,sizeof(col));
	for(int i=p+1;i<=m;i++){
		g.add(edges[i].a,edges[i].b);
		g.add(edges[i].b,edges[i].b);
	}
	for(int i=1;i<=n;i++){
		if(col[i]==0){
			col[i]=1;
			if(!dfs(i,0,1)){
				return 0;
			}
		}
	}
	return 1;
}
signed main(){
	cin>>n>>m;
	for(int i=1;i<=m;i++){
		cin>>edges[i].a>>edges[i].b>>edges[i].c;
	}
	sort(edges+1,edges+m+1,cmp);
	int l=1,r=m,mid;
	while(l<r){
		mid=(l+r)>>1;
		if(check(mid))r=mid;
		else l=mid+1;
		cout<<l<<" "<<r<<endl;
	}
	if(m==1)cout<<0<<endl;
	else cout<<edges[l].c<<endl;
	return 0;
}

回复

2 条回复,欢迎继续交流。

正在加载回复...