The meaning of problems
Point to a root of the tree with a weight of 1, point \ (I \) answer to the exclusive OR of all simple paths and the maximum value, and these paths \ (LCA \) of \ (I \) , seek answer each point
Thinking
Solving the problem must first know and value of any exclusive or a simple path tree how demand
Since the path seeking simple XOR and, a routine of recording a path to the root node points and XOR, referred to as \ (w [i] \) , then a path and is the exclusive OR \ (w [i] \) ^ \ (W [J] \) ^ \ (A [LCA (I, J)] \)
Suppose now required \ (ANS [i] \) (with i is \ (lca \) all the paths) that need to be addressed through all the \ (i \) path point, easy to think to build a \ (trie \ ) tree, each a sub-tree in the i \ (tire \) tree query, and then to \ (trie \) added Fengyun sub-tree, you can count all too \ (i \) path points (It is also common routines treated all paths of a point of it)
In the above process, a child node of the tree are each \ (ASK \) a \ (INS \) a, the processing complexity of all subtrees is the \ (O (nlog ^ 2N) \) ~ \ (O (n ^ 2logn) \ )
Found built in dealing with a sub-tree \ (trie \) tree can be used directly for his father, then the son of the greatest of all sub-tree built \ (trie \) passed to his father, the largest sub-tree That heavy son, do so in total each weighing son will only be processed once, does not depend on the complexity of the heavy son, so I dropped \ (O (nlog ^ 2n) \) (heuristic is merged)
Of course, this thing can also be called dsu on tree
Code
#include<bits/stdc++.h>
#define N 100005
#define Max(x,y) ((x)>(y)?(x):(y))
#define Min(x,y) ((x)<(y)?(x):(y))
using namespace std;
int n,a[N],ans[N];
int size[N],son[N],w[N];
int ndsum,root[N],nxt[N*31][2],temp=30;
struct Edge
{
int next,to;
}edge[N<<1];int head[N],cnt=1;
void add_edge(int from,int to)
{
edge[++cnt].next=head[from];
edge[cnt].to=to;
head[from]=cnt;
}
template <class T>
void read(T &x)
{
char c;int sign=1;
while((c=getchar())>'9'||c<'0') if(c=='-') sign=-1; x=c-48;
while((c=getchar())>='0'&&c<='9') x=x*10+c-48; x*=sign;
}
void DFS(int rt,int fa)
{
w[rt]=w[fa]^a[rt];
size[rt]=1;
for(int i=head[rt];i;i=edge[i].next)
{
int v=edge[i].to;
if(v==fa) continue;
DFS(v,rt);
size[rt]+=size[v];
if(size[son[rt]]<size[v]) son[rt]=v;
}
}
int ask(int rt,int x)
{
int now=root[rt],ret=0;
for(int i=temp;i>=0;--i)
{
int c=(x>>i & 1);
if(nxt[now][!c])
{
ret+=(1<<i);
now=nxt[now][!c];
}
else now=nxt[now][c];
}
return ret;
}
void ins(int rt,int x)
{
if(!root[rt]) root[rt]=++ndsum;
int now=root[rt];
for(int i=temp;i>=0;--i)
{
int c=(x>>i & 1);
if(!nxt[now][c]) nxt[now][c]=++ndsum;
now=nxt[now][c];
}
}
void ii(int rt,int fa,int rot)
{
ins(rot,w[rt]);
for(int i=head[rt];i;i=edge[i].next)
{
int v=edge[i].to;
if(v==fa) continue;
ii(v,rt,rot);
}
}
void qq(int rt,int fa,int rot)
{
ans[rot]=Max(ans[rot],ask(rot,w[rt]^a[rot]));
for(int i=head[rt];i;i=edge[i].next)
{
int v=edge[i].to;
if(v==fa) continue;
qq(v,rt,rot);
}
}
void dfs(int rt,int fa)
{
if(son[rt]) dfs(son[rt],rt),root[rt]=root[son[rt]];//先处理重儿子
ans[rt]=Max(ans[rt],ask(rt,w[rt]^a[rt]));
ins(rt,w[rt]);
for(int i=head[rt];i;i=edge[i].next)
{
int v=edge[i].to;
if(v==fa||v==son[rt]) continue;
int now=ndsum;
dfs(v,rt);//处理轻儿子
for(int j=now+1;j<=ndsum;++j) nxt[j][0]=nxt[j][1]=0;//还原
ndsum=now;
qq(v,rt,rt);
ii(v,rt,rt);
}
}
int main()
{
// freopen("irregular.in","r",stdin);
// freopen("irregular.out","w",stdout);
read(n);
for(int i=1;i<=n;++i) read(a[i]),ans[i]=a[i];
for(int i=1;i<n;++i)
{
int x,y;
read(x);read(y);
add_edge(x,y);
add_edge(y,x);
}
DFS(1,0); dfs(1,0);
for(int i=1;i<=n;++i) printf("%d ",ans[i]);
return 0;
}