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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
| #include<bits/stdc++.h> using namespace std; const int N=1e3+5; const int M=2e6+5; int n,m; struct Edge{ int x,y,c; }e[N];int pf[N]; bool cmp(Edge x,Edge y){return x.c<y.c;} bool in[N],col[N];
int fa[N]; int findfa(int x){return fa[x]==x?x:fa[x]=findfa(fa[x]);} void mer(int x,int y){ x=findfa(x);y=findfa(y); if(x!=y)fa[x]=y; } bool pd(int x,int y){return findfa(x)==findfa(y);} void uinit(){ for(int i=1;i<=n;i++)fa[i]=i; } struct node{ int y,next; }a[M];int len,las[N]; void ins(int x,int y){a[++len]={y,las[x]};las[x]=len;} int typ[N]; void build(int lim){ len=0; memset(las+1,0,sizeof(int)*lim); memset(typ+1,0,sizeof(int)*lim); for(int i=1;i<=lim;i++){ if(!in[i])continue; uinit(); for(int j=1;j<=lim;j++){ if(i!=j && in[j])mer(e[j].x,e[j].y); } for(int j=1;j<=lim;j++){ if(in[j])continue; if(!pd(e[j].x,e[j].y))ins(i,j); if(e[i].c==e[j].c || !col[e[j].c])ins(j,i); } } uinit(); for(int i=1;i<=lim;i++){ if(in[i])mer(e[i].x,e[i].y); } for(int i=1;i<=lim;i++){ if(in[i])continue; if(!pd(e[i].x,e[i].y))typ[i]|=1; if(!col[e[i].c])typ[i]|=2; } }
queue<int> q; int pre[N]; bool solve(int lim){ for(int i=1;i<=lim;i++)pre[i]=-1; for(int i=1;i<=lim;i++){ if(typ[i]&1){ pre[i]=0; q.push(i); } } while(!q.empty()){ int x=q.front();q.pop(); if(typ[x]&2){ while(!q.empty())q.pop(); while(x){ col[e[x].c]^=1; in[x]^=1; x=pre[x]; } return 1; } for(int k=las[x];k;k=a[k].next){ int y=a[k].y; if(pre[y]==-1){ pre[y]=x; q.push(y); } } } return 0; } int main(){ scanf("%d%d",&n,&m); for(int i=1;i<=m;i++){ scanf("%d%d%d",&e[i].x,&e[i].y,&e[i].c); } sort(e+1,e+m+1,[](Edge x,Edge y){ return x.c<y.c; }); for(int i=1;i<=m;i++)pf[e[i].c]=i; for(int i=1;i<=n;i++)pf[i]=max(pf[i],pf[i-1]); for(int i=0;i<=n;i++){ build(pf[i]); if(!solve(pf[i])){ printf("%d\n",i); break; } } return 0; }
|