题意
给定一张无向图,求1->n的路径边权最大异或和。
分析
首先跑一颗任意生成树出来(DFS即可)
然后对于所有非树边,都形成一个环,我们叫它基本环。
通过异或的性质我们发现答案是选取一些基本环异或1~n在树上的路径异或和
所以我们把所有的基本环加入线性基,然后用1~n的路径去匹配贪心找最大值
基本环的寻找实际上结合在DFS中,用异或的性质可以算长度(不要去找LCA)
代码
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn=5e4+105,maxm=1e5+105;
struct LinearBasis{
static const int maxn=65,oo=60;
LL d[maxn];
void Initial()
{
memset(d,0,sizeof(d));
}
bool Insert(LL val)
{
for(int i=oo;i>=0;i--)
{
if(val&(1ll<<i))
{
if(!d[i])
{
d[i]=val;
break;
}
val^=d[i];
}
}
return val;
}
LL query_max(LL n)
{
LL ret=n;
for(int i=oo;i>=0;i--)
ret=max(ret,ret^d[i]);
return ret;
}
}lb;
int np,first[maxn];
struct edge{
int to,next;
LL w;
}E[maxm<<1];
void add(int u,int v,LL w)
{
E[++np]=(edge){v,first[u],w};
first[u]=np;
}
LL dist[maxn];
bool vis[maxn];
void DFS(int i,LL d)
{
vis[i]=1;
dist[i]=d;
for(int p=first[i];p;p=E[p].next)
{
int j=E[p].to;
if(vis[j])
{
lb.Insert(dist[i]^dist[j]^E[p].w);
continue;
}
DFS(j,d^E[p].w);
}
}
int n,m;
int main()
{
//freopen("in.txt","r",stdin);
int u,v;LL w;
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
scanf("%d%d%lld",&u,&v,&w);
add(u,v,w);
add(v,u,w);
}
lb.Initial();
DFS(1,0);
printf("%lld\n",lb.query_max(dist[n]));
return 0;
}