题意:
求无向图1~n的路径最大异或值。
题解:
其实也不是很难吧,然而想不出来,太菜。
当以1为根搜索,得出一个搜索树,那么可以发现,一定是搜索树上的那条链+若干环组成的,然后线性基求最大值。
code:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#define LL long long
using namespace std;
struct node{
LL y,next,c,d;
}a[200010];LL len=0,last[50010];
bool check[100010];
LL A[100010],C[50010],vis[50010],num=0,b[62];
LL n,m;
void ins(LL x,LL y,LL c,LL d)
{
a[++len].y=y;a[len].c=c;a[len].d=d;
a[len].next=last[x];last[x]=len;
}
void dfs(LL x,LL fa,LL c)
{
C[x]=C[fa]^c;vis[x]=true;
for(LL i=last[x];i;i=a[i].next)
{
if(check[a[i].c]) continue;
check[a[i].c]=true;
LL y=a[i].y;
if(!vis[y]) dfs(y,x,a[i].d);
else A[++num]=C[x]^C[y]^a[i].d;
}
}
void ins(LL x)
{
for(LL i=61;i>=0;i--)
{
if((x>>i)&1)
{
if(!b[i]) {b[i]=x;return;}
else x^=b[i];
}
}
}
int main()
{
scanf("%lld %lld",&n,&m);
for(LL i=1;i<=m;i++)
{
LL x,y,d;scanf("%lld %lld %lld",&x,&y,&d);
ins(x,y,i,d);ins(y,x,i,d);
}
memset(check,false,sizeof(check));
memset(vis,false,sizeof(vis));
dfs(1,0,0);
for(LL i=1;i<=num;i++) ins(A[i]);
for(LL i=61;i>=0;i--)
if((C[n]^b[i])>C[n]) C[n]^=b[i];
printf("%lld",C[n]);
}