BZOJ2115: [Wc2011] Xor

题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2115

一条路径就是一条1到n的路径加上一坨环。因为是xor运算就二进制拆位,维护61个向量的线性基就可以了。

#include<cstring>
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#define rep(i,l,r) for (int i=l;i<=r;i++)
#define down(i,l,r) for (int i=l;i>=r;i--)
#define clr(x,y) memset(x,y,sizeof(x))
#define inf int(1e9)
#define maxn 100500
#define mm 1000000007
#define ll long long
using namespace std;
struct data{int obj,pre; ll c;
}e[maxn*2];
int n,m,tot,cnt,cir,head[maxn],vis[maxn];
ll d[maxn],ans,a[500500];
ll bin[65];
int read(){
    int x=0,f=1; char ch=getchar();
    while (!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
    while (isdigit(ch)) {x=x*10+ch-'0'; ch=getchar();}
    return x*f;
}
void insert(int x,int y,ll z){
    e[++tot].obj=y; e[tot].c=z; e[tot].pre=head[x]; head[x]=tot;
}
void dfs(int u){
    vis[u]=1;
    for (int j=head[u];j;j=e[j].pre){
        int v=e[j].obj;
        if (!vis[v]) d[v]=d[u]^e[j].c,dfs(v);
        else a[++cir]=d[v]^d[u]^e[j].c;
    }
}
void gs(){
    int tmp;
    down(i,60,0){
        ll now=bin[i];
        tmp=cnt+1;
        while (tmp<=cir&&!(a[tmp]&now)) tmp++;
        if (tmp==cir+1) continue;
        cnt++;
        swap(a[cnt],a[tmp]);
        rep(j,1,cir) if (j!=cnt&&(a[j]&now)) a[j]^=a[cnt];
    }
}
int main(){
    n=read(); m=read();
    bin[0]=1; rep(i,1,60) bin[i]=bin[i-1]<<1;
    ll z;
    rep(i,1,m){
        int x=read(),y=read(); scanf("%lld",&z);
        insert(x,y,z); insert(y,x,z);
    }
    dfs(1); gs();
    ans=d[n];
    rep(i,1,cnt) ans=max(ans,ans^a[i]);
    printf("%lld\n",ans);
    return 0;
}

 

posted on 2015-12-16 13:08  ctlchild  阅读(170)  评论(0编辑  收藏  举报

导航