[高斯消元 线性基] BZOJ 2115 [Wc2011] Xor

来源:互联网 发布:王珊数据库自学材料 编辑:程序博客网 时间:2024/06/06 00:11
任意两条路径的和为一个环

任取一条1-N的路,找一个环与其XOR和最大

莫队 Orz


 


#include<cstdio>#include<cstdlib>#include<algorithm>using namespace std;typedef long long ll;inline char nc(){    static char buf[100000],*p1=buf,*p2=buf;    if (p1==p2) { p2=(p1=buf)+fread(buf,1,100000,stdin); if (p1==p2) return EOF; }    return *p1++;}inline void read(int &x){    char c=nc(),b=1;    for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;    for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;}inline void read(ll &x){    char c=nc(),b=1;    for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;    for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;}const int N=50005,M=100005;struct edge{    int u,v,next; ll w;}G[M<<1];int head[N],inum;inline void add(int u,int v,ll w,int p){    G[p].u=u; G[p].v=v; G[p].w=w; G[p].next=head[u]; head[u]=p;}int n,m,tot;ll depth[N];int vst[N];ll a[M<<1];#define V G[p].vinline void dfs(int u,int fa){    vst[u]=1;    for (int p=head[u];p;p=G[p].next)        if (p!=(fa^1))        {            if (!vst[V])                depth[V]=depth[u]^G[p].w,dfs(V,p);            else                a[++tot]=depth[V]^depth[u]^G[p].w;        }}inline int Gauss(){    int k=1;    for (int p=63;~p;p--)    {        int t=0;        for (int i=k;i<=tot && !t;i++) if (a[i]>>p&1) t=i;        if (!t) continue;        swap(a[t],a[k]);        for (int i=1;i<=tot;i++)            if (i!=k && a[i]>>p&1)                a[i]^=a[k];        k++;    }    return k--;}int main(){    int iu,iv; ll iw;    freopen("t.in","r",stdin);    freopen("t.out","w",stdout);    read(n); read(m);    for (int i=1;i<=m;i++)        read(iu),read(iv),read(iw),add(iu,iv,iw,++inum),add(iv,iu,iw,++inum);    dfs(1,0);    tot=Gauss();    ll ans=depth[n];    for (int i=1;i<=tot;i++)        if ((ans^a[i])>ans)            ans^=a[i];    printf("%lld\n",ans);    return 0;}


0 0