[SCOI2016]幸运数字
作者:互联网
算简单的了,直接拿线性基维护路径异或然后倍增跳合并就行。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
#include<iostream>
using namespace std;
#define re register
#define ll long long
inline int gi(){
int f=1,sum=0;char ch=getchar();
while(ch>'9' || ch<'0'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0' && ch<='9'){sum=(sum<<3)+(sum<<1)+ch-'0';ch=getchar();}
return f*sum;
}
inline ll gl(){
ll f=1,sum=0;char ch=getchar();
while(ch>'9' || ch<'0'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0' && ch<='9'){sum=(sum<<3)+(sum<<1)+ch-'0';ch=getchar();}
return f*sum;
}
const int N=20010;
struct xxj{
ll p[62];
void insert(ll &x){
for(int i=61;~i;i--)
if((x>>i)&1){
if(!p[i]){p[i]=x;return;}
x^=p[i];
}
return;
}
void merge(xxj &b){
for(int i=61;~i;i--)
if(b.p[i])insert(b.p[i]);
return;
}
}xj[N][17],ans;//15
int f[N][17],n,Q,front[N],cnt;
struct node{int to,nxt;}e[400010];
void Add(int u,int v){e[++cnt]=(node){v,front[u]};front[u]=cnt;}
ll g[N];
int dep[N];
void dfs(int u,int ff){
f[u][0]=ff;xj[u][0].insert(g[u]);dep[u]=dep[ff]+1;
for(int i=front[u];i;i=e[i].nxt){
int v=e[i].to;if(v==ff)continue;
dfs(v,u);
}
}
void lca(int u,int v){
if(dep[u]<dep[v])swap(u,v);
memset(ans.p,0,sizeof(ans.p));
for(int i=15;~i;i--)
if(dep[u]-(1<<i)>=dep[v]){
ans.merge(xj[u][i]);
u=f[u][i];
}
if(u==v){
ans.merge(xj[u][0]);
return;
}
for(int i=15;~i;i--)
if(f[u][i]!=f[v][i]){
ans.merge(xj[u][i]);ans.merge(xj[v][i]);
u=f[u][i],v=f[v][i];
}
ans.merge(xj[u][0]);ans.merge(xj[v][0]);
ans.merge(xj[f[u][0]][0]);
}
int main(){
n=gi();Q=gi();
for(int i=1;i<=n;i++)g[i]=gl();
for(int i=1;i<n;i++){int u=gi(),v=gi();Add(u,v);Add(v,u);}
dfs(1,1);
for(int j=1;j<=15;j++)
for(int i=1;i<=n;i++){
f[i][j]=f[f[i][j-1]][j-1];
xj[i][j]=xj[i][j-1];
xj[i][j].merge(xj[f[i][j-1]][j-1]);
}
while(Q--){
int u=gi(),v=gi();
lca(u,v);
ll sum=0;
for(int i=61;~i;i--)
if(ans.p[i])sum=max(sum,sum^(ll)ans.p[i]);
printf("%lld\n",sum);
}
return 0;
}
标签:xj,ch,数字,int,merge,ans,SCOI2016,幸运,include 来源: https://www.cnblogs.com/mle-world/p/11146016.html