bzoj2115 [Wc2011] Xor

[Wc2011] Xor

Time Limit: 10 Sec Memory Limit: 259 MB

Description

Input

第一行包含两个整数N和 M, 表示该无向图中点的数目与边的数目。 接下来M 行描述 M 条边,每行三个整数Si,Ti ,Di,表示 Si 与Ti之间存在 一条权值为 Di的无向边。 图中可能有重边或自环。

Output

仅包含一个整数,表示最大的XOR和(十进制结果),注意输出后加换行回车。

Sample Input

5 7
1 2 2
1 3 2
2 4 1
2 5 1
4 5 3
5 3 4
4 3 2

Sample Output

6
HINT

这道题是真的好。。。。
但是233
我没有写。。。。

先说思路,随便一条路1到n你先算出来,然后你会发现如果你要在中途走其他路的话,考虑异或的特殊性和对答案的贡献,实际上等效于你在答案上直接异或一个环的贡献。。。
这是因为如果这个环的一部分在你的路上,你异或一下等于你走的是这个环你没有走的一部分,原来的那部分你又异或了一次就抵消了。如果你是绕路去的话,就是直接多走了一个环,而你去的路因为你回来又走了一次,就没有贡献了。

所以一共两个部分,dfs找环,然后随便找一条路。

问题变成你有一个数,然后另外有一个数的集合。用你的数去异或这个集合中的数,问最大是多少?
线性基板题了,我昨天才打过,我会。
dfs,目测5行,我会。
综上我都会,所以。。。。
我口胡啦!!!

让我贴题解吧!!!!啦啦啦


#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <set>
using namespace std;
typedef long long LL;
const int MAXN = 50011;
const int MAXM = 200011;
int n,m,ecnt;
int first[MAXN],next[MAXM],to[MAXM];
LL w[MAXM],dx[MAXN];
bool vis[MAXN];
int cnt;
LL circle[MAXM],ans;//经过每个环可获得的的权值
LL p[63];

inline int getint(){int w=0,q=0;char c=getchar();while((c<'0'||c>'9')&&c!='-')c=getchar();if(c=='-')q=1,c=getchar();while (c>='0' && c<='9') w=w*10+c-'0', c=getchar(); return q ? -w : w;}
inline LL getlong(){LL w=0,q=0;char c=getchar();while((c<'0' || c>'9')&&c!='-')c=getchar();if(c=='-') q=1,c=getchar();while (c>='0' && c<='9') w=w*10+c-'0', c=getchar(); return q ? -w : w;}

inline void dfs(int x){
    vis[x]=1;
    for(int i=first[x];i;i=next[i]) {
    int v=to[i]; 
    if(!vis[v]) dx[v]=dx[x]^w[i],dfs(v);
    else circle[++cnt]=dx[v]^dx[x]^w[i];
    }
}

inline void work(){
    n=getint(); m=getint(); int x,y; LL z;
    for(int i=1;i<=m;i++) {
    x=getint(); y=getint(); z=getlong();
    next[++ecnt]=first[x]; first[x]=ecnt; to[ecnt]=y; w[ecnt]=z;
    next[++ecnt]=first[y]; first[y]=ecnt; to[ecnt]=x; w[ecnt]=z;
    }
    dfs(1);
    ans=dx[n];//任取一条从1到n的路径,并得到其xor和
    for(int i=1;i<=cnt;i++)//构造线性基
    for(int j=62;j>=0;j--) {
        if(!(circle[i]>>j)) continue;
        if(!p[j]) { p[j]=circle[i]; break; }
        circle[i]^=p[j];
    }
    //for(int i=62;i>=0;i--) if(!(ans>>i)) ans^=p[i];
    //ans有初值,不能直接根据这一位是否为0来判断是否更大,max更为稳妥
    for(int i=62;i>=0;i--) if((ans^p[i])>ans) ans=ans^p[i];//从线性基中得到最大值
    printf("%lld",ans);
}

int main()
{
  work();
  return 0;
}

posted @ 2018-04-03 20:36  沛霖  阅读(132)  评论(0编辑  收藏  举报