您的位置:首页 > 其它

【BZOJ】【P2115】【Wc2011】【Xor】【题解】【线性基】

2014-12-15 22:01 330 查看
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2115

自行百度:"莫涛 xor"

还有发现了一个非常简单的线性基求法

http://sujinyue.is-programmer.com/posts/42663.html

Code:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<cctype>
using namespace std;
const int maxn=50010;
typedef unsigned long long LL;
struct edge{int u,v;LL w;};
vector<edge>G[maxn];
LL a[maxn<<2],w[maxn],base[64];
int vis[maxn],size,n,m;
template<class T>
T getint(){
T res=0;char c=getchar();
while(!isdigit(c))c=getchar();
while(isdigit(c))res=res*10+c-'0',c=getchar();
return res;
}
void dfs(int u){
for(int i=0;i<G[u].size();i++){
edge e=G[u][i];
if(!vis[e.v]){
vis[e.v]=1;
w[e.v]=w[u]^e.w;
dfs(e.v);
}else a[++size]=w[u]^w[e.v]^e.w;
}
}
int main(){
n=getint<int>();m=getint<int>();
for(int i=1;i<=m;i++){
int u=getint<int>(),v=getint<int>();
LL w=getint<LL>();
G[u].push_back((edge){u,v,w});
G[v].push_back((edge){v,u,w});
}vis[1]=1;dfs(1);
for(int i=1;i<=size;i++)
for(int j=63;j>=0;j--){
if(a[i]>>j&1){
if(!base[j]){base[j]=a[i];break;}
else a[i]^=base[j];
}
}LL ans=w
;
for(int i=63;i>=0;i--)ans=max(ans,ans^base[i]);
cout<<ans<<endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  bzoj