一道并查集水题,按值从大到小排序之后,乱搞。
扩展域

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct node
{
    int x,y,c;
}a[110000];
inline bool cmp(node x,node y){return x.c>y.c;}
int avoid[110000];
int fa[21000];
int findfa(int x)
{
    return fa[x]==x?x:fa[x]=(findfa(fa[x]));
}
int main()
{
    int n,m;scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].c);
    }
    sort(a+1,a+m+1,cmp);
    for(int i=1;i<=n;i++)fa[i]=i;
    for(int i=1;i<=m+1;i++)
    {
        int fx=findfa(a[i].x),fy=findfa(a[i].y);
        if(fx==fy){printf("%d\n",a[i].c);return 0;}
        else
        {
            if(!avoid[a[i].x])avoid[a[i].x]=a[i].y;
            else fa[findfa(avoid[a[i].x])]=fa[findfa(fy)];
            if(!avoid[a[i].y])avoid[a[i].y]=a[i].x;
            else fa[findfa(avoid[a[i].y])]=fa[findfa(fx)];
        }
    }
    return 0;
}

边带权

#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<cstring>
#define gc getchar()
using namespace std;
const int N=2e4+5;
const int M=1e5+5;
inline void qr(int &x)
{
    char c=gc;int f=1;x=0;
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=gc;}
    while(c>='0'&&c<='9'){x=x*10+(c^48);c=gc;}
    x*=f;
}
void qw(int x)
{
    if(x<0)x=-x,putchar('-');
    if(x/10)qw(x/10);
    putchar(x%10+48);
}
struct edge{int x,y,d;bool operator <(const edge a)const{return d>a.d;}}a[M];
int fa[N],d[N];
int get(int x)
{
    if(fa[x]==x)return x;
    int root=get(fa[x]);
    d[x]^=d[fa[x]];
    return fa[x]=root;
}
int main()
{
    int n,m;qr(n),qr(m);
    for(int i=1;i<=n;i++)fa[i]=i;
    for(int i=1;i<=m;i++)qr(a[i].x),qr(a[i].y),qr(a[i].d);
    sort(a+1,a+m+1);int mx=0;
    for(int i=1;i<=m;i++)
    {
        int x=a[i].x,y=a[i].y;
        int p=get(x),q=get(y);
        if(p!=q)fa[p]=q,d[p]=1^d[x]^d[y];
        else if((d[x]^d[y])==0){mx=a[i].d;break;}
    }
    printf("%d\n",mx);
    return 0;
}