noip 关押罪犯
正解:咕咕
#include<bits/stdc++.h> using namespace std; const int N=3e4,M=3e5; int vis[N],f[N],n,m,tot,mid,miner=1000000009,maxer=0; struct re{ int d; int v; int nex; }; re edge[M]; void ad(int x,int y,int w){ tot++; edge[tot].v=y; edge[tot].d=w; edge[tot].nex=f[x]; f[x]=tot; } bool jj(int v){ int k=f[v]; while(k!=-1){ int x=edge[k].v; int w=edge[k].d; if(w>mid){ if(vis[v]==vis[x])return 0; if(!vis[x]){ vis[x]=3-vis[v]; if(!jj(x))return 0; } } k=edge[k].nex; } return 1; } bool ok(){ bool az; vis[1]=1; az=jj(1); if(!az)return 0; for(int i=2;i<=n;i++) if(!vis[i]){ vis[i]=1; az=jj(i); if(!az)return 0; } return 1; } int main(){ //freopen("p.in","r",stdin); memset(f,-1,sizeof(f)); cin>>n>>m; for(int i=1;i<=m;i++){ int a,b,c; cin>>a>>b>>c; ad(a,b,c); ad(b,a,c); miner=min(miner,c); maxer=max(maxer,c); } int l=miner,r=maxer; mid=0;if(ok()){cout<<0;return 0;} while(l+1<r){ memset(vis,0,sizeof(vis)); mid=(l+r)>>1; int az=ok(); if(az)r=mid; else l=mid; } int az=ok(); cout<<r; return 0; }
戒骄戒躁