【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;
}
自行百度:"莫涛 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 2115: [Wc2011] Xor 线性基
- BZOJ 2115 [Wc2011] Xor ——线性基
- [BZOJ 2115 Wc2011 Xor]线性基
- [BZOJ 2115][Wc2011] Xor:线性基
- 【BZOJ 2115】[Wc2011] Xor 线性基
- bzoj 2115: [Wc2011] Xor(DFS+线性基)
- BZOJ 2115: [Wc2011] Xor (dfs + gauss 线性基 异或最长路)
- BZOJ 2115: [Wc2011] Xor 线性基
- BZOJ 2115 WC2011 Xor 线性基+贪心
- BZOJ 2115: [Wc2011] Xor DFS + 线性基
- [高斯消元 线性基] BZOJ 2115 [Wc2011] Xor
- BZOJ 2115: [Wc2011] Xor [高斯消元XOR 线性基 图]
- bzoj 2115: [Wc2011] Xor
- BZOJ 2115: [Wc2011] Xor
- 【bzoj2115】[Wc2011] Xor DFS树+高斯消元求线性基
- bzoj2115 [Wc2011] Xor(图论+线性基)
- [DFS树 + 线性基] BZOJ2115: [Wc2011] Xor
- bzoj 2115: [Wc2011] Xor【线性基+dfs】
- BZOJ 2115: [Wc2011] Xor
- 【BZOJ2115】 [Wc2011] Xor——线性基