1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> using namespace std; struct edge{ int v,w,next; }Edge[200005]; int n,m,col[20005],data[200005]; int head[20005],cnt; int l,r,mid,Max; bool book[20005]; inline void AddEdge(int u,int v,int w){ cnt++; Edge[cnt].v=v; Edge[cnt].w=w; Edge[cnt].next=head[u]; head[u]=cnt; return; } bool dfs(int u,int color){ if (book[u]) return col[u]==color; book[u]=true; col[u]=color; bool flag=true; for (int i=head[u];i!=0&&flag;i=Edge[i].next){ int v=Edge[i].v; if (Edge[i].w<=Max) continue; flag=dfs(v,1-color); } return flag; } inline bool Colored(){ memset(book,false,sizeof (book)); memset(col,0,sizeof (col)); Max=data[mid]; for (int i=1;i<=n;i++){ if (book[i]) continue; if (!dfs(i,0)) return false; } return true; } int main(){ int u,v,w; scanf("%d%d",&n,&m); for (int i=1;i<=m;i++){ scanf("%d%d%d",&u,&v,&w); data[i]=w; AddEdge(u,v,w); AddEdge(v,u,w); } sort (data+1,data+m+1); l=0; r=m; while (l<r){ mid=(l+r)>>1; if (Colored()) r=mid; else l=mid+1; } printf ("%d\n",data[l]); return 0; }
|