比赛 |
4043级2023省选练习赛2 |
评测结果 |
AAAAAAAAAA |
题目名称 |
幸运数字 |
最终得分 |
100 |
用户昵称 |
op_组撒头屯 |
运行时间 |
20.916 s |
代码语言 |
C++ |
内存使用 |
140.87 MiB |
提交时间 |
2023-03-06 20:08:29 |
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define ld long double
#define pii pair<int,int>
#define vi vector<int>
#define si set<int>
#define unsi unordered_set<int>
#define qi queue<int>
#define sti stack<int>
#define pqi priority_queue<int>
#define mii map<int,int>
#define unmii unordered_map<int,int>
#define fi first
#define se second
#define pb push_back
#define clr(f,n) memset(f,0,sizeof(int)*(n))
#define cpy(f,g,n) memcpy(f,g,sizeof(int)*(n))
const int N=20000+5;
const int M=200000+5;
struct sdf{
int to,next;
}eg[2*N];
int head[N],ne=0;
void add(int x,int y){
eg[++ne]={y,head[x]};
head[x]=ne;return ;
}
int n,m,lgn;
void insert(ll *f,ll g){
for (int i=60;i>=0;i--){
if (g&(1ll<<i)){
if (!f[i]){f[i]=g;return ;}
g^=f[i];
}
}
}
void merge(ll *f,ll *g,ll *h){
for (int i=60;i>=0;i--)f[i]=g[i];
for (int i=60;i>=0;i--){
if (!h[i])continue;
insert(f,h[i]);
}
}
int fa[N][16],dep[N];
ll a[N],f[N][16][61];
void dfs(int pt,int fat){
fa[pt][0]=fat;insert(f[pt][0],a[pt]);dep[pt]=dep[fat]+1;
for (int i=head[pt];i!=0;i=eg[i].next){
int v=eg[i].to;
if (v==fat)continue;
dfs(v,pt);
}
}
ll t[61];
ll calc(){
ll ans=0;
for (int i=60;i>=0;i--)ans=max(ans,ans^t[i]);
return ans;
}
ll query(int x,int y){
memset(t,0,sizeof(t));
if (dep[x]<dep[y])swap(x,y);
for (int i=lgn;i>=0;i--){
if (fa[x][i]&&dep[fa[x][i]]>=dep[y]){
merge(t,t,f[x][i]);x=fa[x][i];
}
if (x==y){
insert(t,a[x]);return calc();
}
}
for (int i=lgn;i>=0;i--){
if (fa[x][i]!=fa[y][i]){
merge(t,t,f[x][i]);merge(t,t,f[y][i]);
x=fa[x][i];y=fa[y][i];
}
}
insert(t,a[x]);insert(t,a[y]);insert(t,a[fa[x][0]]);
return calc();
}
int main(){
freopen ("luckynum.in","r",stdin);
freopen ("luckynum.out","w",stdout);
scanf("%d%d",&n,&m);lgn=1.0*log(n)/log(2);
for (int i=1;i<=n;i++)scanf("%lld",&a[i]);
for (int i=1;i<n;i++){
int x,y;scanf("%d%d",&x,&y);
add(x,y);add(y,x);
}
dfs(1,0);
for (int i=1;i<=lgn;i++){
for (int j=1;j<=n;j++){
if (!fa[j][i-1])continue;
fa[j][i]=fa[fa[j][i-1]][i-1];
merge(f[j][i],f[j][i-1],f[fa[j][i-1]][i-1]);
}
}
for (int i=1;i<=m;i++){
int x,y;scanf("%d%d",&x,&y);
printf("%lld\n",query(x,y));
}
}