WC2011 XOR

题目链接

题意简述

求一条\(1\sim n\)的路径,使路径上的边权异或和最大.

解析

一条路径可以视为\(1\sim n\)的简单路径加上某些环.
可能我们一开始选的链不够优秀,但是我们能加上某些环来优化答案.
如果有一个很远的环,我们可以从链上走过去,遍历整个环,然后再走回来.
走过去走回来的这一段的\(xor\)值显然抵消了,于是只剩下了一个环.

如果有另一条链,假设比现在的链更优.
那么这一条链显然与现在的链形成了某些环.
那么还是相当于现在的链异或上某些环的权值,然后找到最大值.

因此我们将所有的环搞出来,然后扔进线性基中,然后随便取一条\(1\sim n\)的路径作为初始答案,在线性基中查询即可.

代码如下

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#define N (100010)
#define M (200010) 
#define inf (0x7f7f7f7f)
#define rg register int
#define Label puts("NAIVE")
#define spa print(' ')
#define ent print('\n')
#define rand() (((rand())<<(15))^(rand()))
typedef long double ld;
typedef long long LL;
typedef unsigned long long ull;
using namespace std;
inline char read(){
    static const int IN_LEN=1000000;
    static char buf[IN_LEN],*s,*t;
    return (s==t?t=(s=buf)+fread(buf,1,IN_LEN,stdin),(s==t?-1:*s++):*s++);
}
template<class T>
inline void read(T &x){
    static bool iosig;
    static char c;
    for(iosig=false,c=read();!isdigit(c);c=read()){
        if(c=='-')iosig=true;
        if(c==-1)return;
    }
    for(x=0;isdigit(c);c=read())x=((x+(x<<2))<<1)+(c^'0');
    if(iosig)x=-x;
}
inline char readchar(){
    static char c;
    for(c=read();!isalpha(c);c=read())
    if(c==-1)return 0;
    return c;
}
const int OUT_LEN = 10000000;
char obuf[OUT_LEN],*ooh=obuf;
inline void print(char c) {
    if(ooh==obuf+OUT_LEN)fwrite(obuf,1,OUT_LEN,stdout),ooh=obuf;
    *ooh++=c;
}
template<class T>
inline void print(T x){
    static int buf[30],cnt;
    if(x==0)print('0');
    else{
        if(x<0)print('-'),x=-x;
        for(cnt=0;x;x/=10)buf[++cnt]=x%10+48;
        while(cnt)print((char)buf[cnt--]);
    }
}
inline void flush(){fwrite(obuf,1,ooh-obuf,stdout);}
int n,m,ne[M],fi[N],b[M],E;
LL dis[N],c[M]; bool vis[N];
struct LB{
    LL a[101];
    void insert(LL x){
        for(int i=62;i>=0;i--){
            if(!((x>>(LL)i)&1ll))continue;
            if(!a[i]){a[i]=x;break;}
            x^=a[i];
        }
    }
    LL getmax(LL x){
        LL ans=x;
        for(int i=62;i>=0;i--)
        if(ans<(ans^a[i]))ans^=a[i];
        return ans;
    }
}S;
void add(int x,int y,int z){
    ne[++E]=fi[x],fi[x]=E,b[E]=y,c[E]=z;
}
void dfs(int u,LL res){
    dis[u]=res,vis[u]=1;
    for(int i=fi[u];i;i=ne[i]){
        int v=b[i];
        if(vis[v])S.insert(res^c[i]^dis[v]);
        else dfs(v,res^c[i]);
    }
}
int main(){
    read(n),read(m);
    for(int i=1;i<=m;i++){
        int x,y; LL z;
        read(x),read(y),read(z);
        add(x,y,z),add(y,x,z);
    }
    dfs(1,0);
    printf("%lld\n",S.getmax(dis[n]));
}

猜你喜欢

转载自www.cnblogs.com/Romeolong/p/10062880.html
今日推荐