description

【JZOJ6357】小ω的图(graph)-LMLPHP


analysis

  • 拆位从高位到低位贪心

  • 对于当前位,如果把所有当前位为\(1\)的边塞入,\(1\)和\(n\)连通,则该位必须为\(1\)

  • 这个是因为高位的\(1\)比所有低位的\(1\)都要优,用并查集维护连通性

  • 对固定下的位,继续向下贪心,找低位中满足所有条件的\(1\)位即可


code

#pragma GCC optimize("O3")
#pragma G++ optimize("O3")
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define MAXN 100005
#define MAXM 500005
#define ll long long
#define reg register ll
#define fo(i,a,b) for (reg i=a;i<=b;++i)
#define fd(i,a,b) for (reg i=a;i>=b;--i) using namespace std; ll x[MAXM],y[MAXM],z[MAXM];
ll fa[MAXN],pow[70],f[70];
ll n,m,ans; inline ll read()
{
ll x=0,f=1;char ch=getchar();
while (ch<'0' || '9'<ch){if (ch=='-')f=-1;ch=getchar();}
while ('0'<=ch && ch<='9')x=x*10+ch-'0',ch=getchar();
return x*f;
}
inline ll getfa(ll x){return !fa[x]?x:fa[x]=getfa(fa[x]);}
inline void link(ll x,ll y){if (getfa(x)!=getfa(y))fa[getfa(x)]=getfa(y);}
int main()
{
//freopen("T1.in","r",stdin);
freopen("graph.in","r",stdin);
freopen("graph.out","w",stdout);
n=read(),m=read(),pow[0]=1;
fo(i,1,62)pow[i]=pow[i-1]*2;
fo(i,1,m)x[i]=read(),y[i]=read(),z[i]=read();
fd(j,62,0)
{
ans+=pow[j],memset(fa,0,sizeof(fa));
fo(i,1,m)if ((ans&z[i])==ans)link(x[i],y[i]);
if (getfa(1)!=getfa(n))ans-=pow[j];
}
printf("%lld\n",ans);
return 0;
}
05-27 13:30