“边带权”并查集,注意合并操作对新连接边权的处理
(图片) https://cdn.luogu.com.cn/upload/image_hosting/egrisdpt.png
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
inline int read()
{
int x=0;bool f=false;
char ch=getchar();
while(ch<'0' || ch>'9') f|=(ch=='-'),ch=getchar();
while(ch>='0'&&ch<='9') x=x*10+(ch^48),ch=getchar();
return f?-x:x;
}
const int N=20010,M=100010;
int a[M],b[M],c[M],num[M];
bool cmp(int x,int y) {return c[x]>c[y];}
int fa[N],d[N];
int findfa(int x)
{
if(fa[x]==x) return x;
int Fa=findfa(fa[x]);
d[x]^=d[fa[x]];
return fa[x]=Fa;
}
int main()
{
int n=read(),m=read();
for(int i=1;i<=n;i++) fa[i]=i,d[i]=0;
for(int i=1;i<=m;i++) a[i]=read(),b[i]=read(),c[i]=read(),num[i]=i;
sort(num+1,num+m+1,cmp);
for(int i=1;i<=m;i++)
{
int x=a[num[i]],y=b[num[i]];
int tx=findfa(x),ty=findfa(y);
if(tx==ty)
{
if(d[x]^d[y]) continue;
else
{
printf("%d\n",c[num[i]]);
return 0;
}
}
else fa[tx]=ty,d[tx]=d[x]^d[y]^1;
}
puts("0");
return 0;
}