题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2115
题意:给出一个带权无向图。求一条1到n的路径使得路径上权值的抑或值最大?
思路:(1)从1DFS,记录每个环的抑或值以及从1到每个点的抑或值f;
(2)令ans=f[n],那么从高位到低位,若ans的某一位为0,看是否能通过某些环使得该位为1。
#include <iostream>
#include <cstdio>
#include <string.h>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <map>
#include <ctype.h>
#include <time.h>
#define abs(x) ((x)>=0?(x):-(x))
#define i64 long long
#define u32 unsigned int
#define u64 unsigned long long
#define clr(x,y) memset(x,y,sizeof(x))
#define CLR(x) x.clear()
#define ph(x) push(x)
#define pb(x) push_back(x)
#define Len(x) x.length()
#define SZ(x) x.size()
#define PI acos(-1.0)
#define sqr(x) ((x)*(x))
#define MP(x,y) make_pair(x,y)
#define EPS 1e-6
#define FOR0(i,x) for(i=0;i<x;i++)
#define FOR1(i,x) for(i=1;i<=x;i++)
#define FOR(i,a,b) for(i=a;i<=b;i++)
#define FORL0(i,a) for(i=a;i>=0;i--)
#define FORL1(i,a) for(i=a;i>=1;i--)
#define FORL(i,a,b)for(i=a;i>=b;i--)
#define rush() int CC;for(scanf("%d",&CC);CC--;)
#define Rush(n) while(scanf("%d",&n)!=-1)
using namespace std;
void RD(int &x){scanf("%d",&x);}
void RD(i64 &x){scanf("%lld",&x);}
void RD(u64 &x){scanf("%I64u",&x);}
void RD(u32 &x){scanf("%u",&x);}
void RD(double &x){scanf("%lf",&x);}
void RD(int &x,int &y){scanf("%d%d",&x,&y);}
void RD(i64 &x,i64 &y){scanf("%lld%lld",&x,&y);}
void RD(u32 &x,u32 &y){scanf("%u%u",&x,&y);}
void RD(double &x,double &y){scanf("%lf%lf",&x,&y);}
void RD(double &x,double &y,double &z){scanf("%lf%lf%lf",&x,&y,&z);}
void RD(int &x,int &y,int &z){scanf("%d%d%d",&x,&y,&z);}
void RD(i64 &x,i64 &y,i64 &z){scanf("%lld%lld%lld",&x,&y,&z);}
void RD(u32 &x,u32 &y,u32 &z){scanf("%u%u%u",&x,&y,&z);}
void RD(char &x){x=getchar();}
void RD(char *s){scanf("%s",s);}
void RD(string &s){cin>>s;}
void PR(int x) {printf("%d\n",x);}
void PR(int x,int y) {printf("%d %d\n",x,y);}
void PR(i64 x) {printf("%lld\n",x);}
void PR(i64 x,i64 y) {printf("%lld %lld\n",x,y);}
void PR(u32 x) {printf("%u\n",x);}
void PR(u64 x) {printf("%llu\n",x);}
void PR(double x) {printf("%.2lf\n",x);}
void PR(double x,double y) {printf("%.5lf %.5lf\n",x,y);}
void PR(char x) {printf("%c\n",x);}
void PR(char *x) {printf("%s\n",x);}
void PR(string x) {cout<<x<<endl;}
void upMin(int &x,int y) {if(x>y) x=y;}
void upMin(i64 &x,i64 y) {if(x>y) x=y;}
void upMin(double &x,double y) {if(x>y) x=y;}
void upMax(int &x,int y) {if(x<y) x=y;}
void upMax(i64 &x,i64 y) {if(x<y) x=y;}
void upMax(double &x,double y) {if(x<y) x=y;}
const int mod=10007;
const i64 inf=((i64)1)<<60;
const double dinf=1000000000000000000.0;
const int INF=100000000;
const int N=50005;
struct node
{
int v,next;
i64 w;
};
node edges[N<<2];
int head[N],e;
void Add(int u,int v,i64 w)
{
edges[e].v=v;
edges[e].w=w;
edges[e].next=head[u];
head[u]=e++;
}
i64 f[N];
vector<i64> V;
int n,m;
void DFS(int u,int t)
{
int i,v;
i64 w;
for(i=head[u];i!=-1;i=edges[i].next)
{
if(i==t) continue;
v=edges[i].v;
w=edges[i].w;
if(f[v]==-1)
{
f[v]=f[u]^w;
DFS(v,i^1);
}
else V.pb(f[v]^f[u]^w);
}
}
int main()
{
RD(n,m);
int i,j,u,v;
i64 w;
clr(head,-1);
FOR1(i,m)
{
RD(u,v);
RD(w);
Add(u,v,w); Add(v,u,w);
}
clr(f,-1); f[1]=0; DFS(1,-1);
i64 p[70]={0},ans=f[n];
FOR0(i,SZ(V)) for(j=63;j>=0;j--)
{
if(V[i]>>j&1)
{
if(p[j]==0)
{
p[j]=V[i];
break;
}
else V[i]^=p[j];
}
}
for(i=62;i>=0;i--) if(!(ans>>i&1)&&p[i]) ans^=p[i];
PR(ans);
}