loj #2013. 「SCOI2016」幸运数字

时间:2023-03-09 22:45:43
loj #2013. 「SCOI2016」幸运数字

#2013. 「SCOI2016」幸运数字

题目描述

A 国共有 n nn 座城市,这些城市由 n−1 n - 1n−1 条道路相连,使得任意两座城市可以互达,且路径唯一。每座城市都有一个幸运数字,以纪念碑的形式矗立在这座城市的正中心,作为城市的象征。一些旅行者希望游览 A 国。旅行者计划乘飞机降落在 x xx 号城市,沿着 x xx 号城市到 y yy 号城市之间那条唯一的路径游览,最终从 y yy 城市起飞离开 A 国。

在经过每一座城市时,游览者就会有机会与这座城市的幸运数字拍照,从而将这份幸运保存到自己身上。然而,幸运是不能简单叠加的,这一点游览者也十分清楚。他们迷信着幸运数字是以异或的方式保留在自己身上的。例如,游览者拍了 3 33 张照片,幸运值分别是 5 55、7 77、11 1111,那么最终保留在自己身上的幸运值就是 9 99(5xor7xor11)。

有些聪明的游览者发现,只要选择性地进行拍照,便能获得更大的幸运值。例如在上述三个幸运值中,只选择 5 55 和 11 1111 ,可以保留的幸运值为 14 1414 。现在,一些游览者找到了聪明的你,希望你帮他们计算出在他们的行程安排中可以保留的最大幸运值是多少。

输入格式

第一行包含两个正整数 n nn、q qq,分别表示城市的数量和旅行者数量。
第二行包含 n nn 个非负整数,其中第 i ii 个整数 Gi G_iG​i​​ 表示 i ii 号城市的幸运值。随后 n−1 n - 1n−1 行,每行包含两个正整数 x xx、y yy,表示 x xx 号城市和 y yy 号城市之间有一条道路相连。
随后 q qq 行,每行包含两个正整数 x xx、y yy,表示这名旅行者的旅行计划是从 x xx 号城市到 y yy 号城市。

输出格式

输出需要包含 q qq 行,每行包含 1 11 个非负整数,表示这名旅行者可以保留的最大幸运值。

样例

样例输入

4 2
11 5 7 9
1 2
1 3
1 4
2 3
1 4

样例输出

14
11

数据范围与提示

N≤20000,Q≤200000,Gi≤260 N \leq 20000, Q \leq 200000, G_i \leq 2 ^ {60}N≤20000,Q≤200000,G​i​​≤2​60​​

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#define maxn 20010
using namespace std;
int n,q,num,head[maxn],fa[maxn][],dep[maxn];
long long st[maxn][][],t[],lb[],ans;
struct node{int to,pre;}e[maxn*];
long long qread(){
long long i=,j=;
char ch=getchar();
while(ch<''||ch>''){if(ch=='-')j=-;ch=getchar();}
while(ch<=''&&ch>=''){i=i*+ch-'';ch=getchar();}
return i*j;
}
void Insert(int from,int to){
e[++num].to=to;
e[num].pre=head[from];
head[from]=num;
}
void dfs(int x){
for(int i=;dep[x]-(<<i)>;i++)
fa[x][i]=fa[fa[x][i-]][i-];
for(int i=head[x];i;i=e[i].pre){
int to=e[i].to;
if(to==fa[x][])continue;
dep[to]=dep[x]+;fa[to][]=x;
dfs(to);
}
}
void merge(long long a[],long long c[]){
for(int i=;i<=;i++)t[i]=a[i];
for(int i=;i<=;i++)
for(int j=;j>=;j--){
if(!t[i])break;
if(t[i]>>j&){
if(!c[j]){c[j]=t[i];break;}
else t[i]^=c[j];
}
}
}
int lca(int x,int y){
if(dep[x]<dep[y])swap(x,y);
for(int i=;i>=;i--)
if(fa[x][i]&&dep[fa[x][i]]>dep[y])
x=fa[x][i];
if(dep[x]>dep[y])x=fa[x][];
if(x==y)return x;
for(int i=;i>=;i--)
if(fa[x][i]&&fa[y][i]&&fa[x][i]!=fa[y][i])
x=fa[x][i],y=fa[y][i];
return fa[x][];
}
int cal(int x,int goal){
if(dep[x]==goal)return x;
for(int i=;i>=;i--)
if(fa[x][i]&&dep[fa[x][i]]>goal)
x=fa[x][i];
return fa[x][];
}
int main(){
scanf("%d%d",&n,&q);
for(int i=;i<=n;i++){
long long x=qread();
for(int j=;j>=;j--)
if(x>>j&){
st[i][][j]=x;
break;
}
}
for(int i=;i<n;i++){
int x=qread(),y=qread();
Insert(x,y);Insert(y,x);
}
dep[]=;
dfs();
for(int i=;<<i<=n;i++)
for(int j=;j<=n;j++)
if(dep[j]-(<<i)>=)
merge(st[j][i-],st[j][i]),
merge(st[cal(j,dep[j]-(<<i-))][i-],st[j][i]);
int x,y,z,p1,p2;
while(q--){
memset(lb,,sizeof(lb));
scanf("%d%d",&x,&y);
ans=;z=lca(x,y);
p1=log2(dep[x]-dep[z]+);
merge(st[x][p1],lb);
merge(st[cal(x,dep[z]+(<<p1)-)][p1],lb);
p2=log2(dep[y]-dep[z]+);
merge(st[y][p2],lb);
merge(st[cal(y,dep[z]+(<<p2)-)][p2],lb);
for(int i=;i>=;i--)
if(!(ans>>i&))ans^=lb[i];
printf("%lld\n",ans);
}
return ;
}