#include <bits/stdc++.h>
//#define int long long
#define double long double
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define pb push_back
#define ALL(i) i.begin(),i.end()
#define gcd(i,j) __gcd(i,j)
#define fi first
#define se second
#define bitCount(i) __builtin_popcount(i)
//#define X first
//#define Y second
#define eps 0.00000001
#define ist insert
//#define mul(a,b,c) (a%c)*(b%c)%c
// #pragma GCC optimize("Ofast","inline","-ffast-math")
// #pragma GCC optimize("O3")
// #pragma GCC target("avx,sse2,sse3,sse4,mmx")
int max(int x,int y){return x>=y?x:y;}
int min(int x,int y){return x>=y?y:x;}
using namespace std;
typedef long long ll;
const int N=100005;
const int M=505;
const int MOD=998244353;//1000000007;
const ll INF=9223372036854775807;//2147483647;
const double PI=acos(-1);
typedef pair<int,int> pii;
typedef pair<double,double> pdd;
typedef pair<double,int> pdi;
struct Trie{
int tot;
vector<int> trie[2];
vector<int> cnt;
void init(int _n){
trie[0].resize(N*100);
trie[1].resize(N*100);
cnt.resize(N*100);
tot=1;
}
void add(int x){
int cur=1;
for (int i=30;i>=0;i--){
int idx=((1<<i)&x)!=0;
if (trie[idx][cur]==0) trie[idx][cur]=++tot;
cur=trie[idx][cur];
cnt[cur]++;
// assert(tot<N*100);
}
}
void del(int x){
int cur=1;
for (int i=30;i>=0;i--){
int idx=((1<<i)&x)!=0;
if (trie[idx][cur]==0) trie[idx][cur]=++tot;
cur=trie[idx][cur];
cnt[cur]--;
}
// assert(tot<N*100);
}
int que(int x){
int res=0, cur=1;
for (int i=30;i>=0;i--){
int idx=((1<<i)&x)!=0;
if (trie[!idx][cur]!=0&&cnt[trie[!idx][cur]]!=0) {
res|=(1<<i);
cur=trie[!idx][cur];
}
else cur=trie[idx][cur];
}
return res;
}
};
int n,K;
int c[N], a[N];
vector<int> e[N];
int hson[N], sz[N];
int clr[N];
int cnt[N*32]{};
int ans[N];
Trie subtree, root;
void dfs1(int v,int pre){
int mx=0,son=0;
for (int i:e[v]) if (i!=pre){
dfs1(i,v);
if (sz[i]>mx){
mx=sz[i];
son=i;
}
sz[v]+=sz[i];
}
hson[v]=son;
sz[v]++;
}
void add(int v){
root.del(clr[c[v]]);
clr[c[v]]^=a[v];
root.add(clr[c[v]]);
subtree.add(a[v]);
}
void del(int v){
root.del(clr[c[v]]);
clr[c[v]]^=a[v];
root.add(clr[c[v]]);
subtree.del(a[v]);
}
void DDel(int v,int pre){
for (int i:e[v]) if (i!=pre) DDel(i,v);
del(v);
}
void AAdd(int v,int pre){
for (int i:e[v]) if (i!=pre) AAdd(i,v);
add(v);
}
void calc(int v,int pre){
for (int i:e[v]) if (i!=pre && i!=hson[v]){
calc(i, v);
DDel(i,v);
}
if (hson[v]!=0) calc(hson[v], v);
for (int i:e[v]) if (i!=pre && i!=hson[v]){
AAdd(i,v);
}
int ans1=subtree.que(clr[c[v]]^a[v]);
int ans2=root.que(a[v]);
add(v);
if (K==1) ans[v]=max(ans1, ans2);
else ans[v]=clr[c[v]];
}
void sol(){
cin >>n>>K;
for (int i=1;i<=n;i++) cin >>c[i];
for (int i=1;i<=n;i++) cin >>a[i];
for (int i=1,x,y;i<n;i++){
cin >>x>>y;
e[x].pb(y);
e[y].pb(x);
}
subtree.init(n); subtree.add(0);
root.init(n); for (int i=1;i<=n;i++) root.add(0);
dfs1(1,0);
calc(1,0);
for (int i=1;i<=n;i++) cout <<ans[i]<<" \n"[i==n];
}
signed main(){
IOS
//srand(time(NULL));
int _=1;
// cin >>_;
while (_--) sol();
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+Ci8vI2RlZmluZSBpbnQgbG9uZyBsb25nCiNkZWZpbmUgZG91YmxlIGxvbmcgZG91YmxlCiNkZWZpbmUgSU9TIGlvczo6c3luY193aXRoX3N0ZGlvKGZhbHNlKTtjaW4udGllKDApO2NvdXQudGllKDApOwojZGVmaW5lIHBiIHB1c2hfYmFjawojZGVmaW5lIEFMTChpKSBpLmJlZ2luKCksaS5lbmQoKQojZGVmaW5lIGdjZChpLGopI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l6ZShOKjEwMCk7CiAgICAgICAgdHJpZVsxXS5yZXNpemUoTioxMDApOwogICAgICAgIGNudC5yZXNpemUoTioxMDApOwogICAgICAgIHRvdD0xOwogICAgfQogICAgdm9pZCBhZGQoaW50IHgpewogICAgICAgIGludCBjdXI9MTsKICAgICAgICBmb3IgKGludCBpPTMwO2k+PTA7aS0tKXsKICAgICAgICAgICAgaW50IGlkeD0oKDE8PGkpJngpIT0wOwogICAgICAgICAgICBpZiAodHJpZVtpZHhdW2N1cl09PTApIHRyaWVbaWR4XVtjdXJdPSsrdG90OwogICAgICAgICAgICBjdXI9dHJpZVtpZHhdW2N1cl07CiAgICAgICAgICAgIGNudFtjdXJdKys7CiAgICAgICAgICAgIC8vIGFzc2VydCh0b3Q8TioxMDApOwogICAgICAgIH0gICAgCiAgICB9CiAgICB2b2lkIGRlbChpbnQgeCl7CiAgICAgICAgaW50IGN1cj0xOwogICAgICAgIGZvciAoaW50IGk9MzA7aT49MDtpLS0pewogICAgICAgICAgICBpbnQgaWR4PSgoMTw8aSkmeCkhPTA7CiAgICAgICAgICAgIGlmICh0cmllW2lkeF1bY3VyXT09MCkgdHJpZVtpZHhdW2N1cl09Kyt0b3Q7CiAgICAgICAgICAgIGN1cj10cmllW2lkeF1bY3VyXTsKICAgICAgICAgICAgY250W2N1cl0tLTsKICAgICAgICB9CiAgICAgICAgLy8gYXNzZXJ0KHRvdDxOKjEwMCk7CiAgICB9CiAgICBpbnQgcXVlKGludCB4KXsKICAgICAgICBpbnQgcmVzPTAsIGN1cj0xOwogICAgICAgIGZvciAoaW50IGk9MzA7aT49MDtpLS0pewogICAgICAgICAgICBpbnQgaWR4PSgoMTw8aSkmeCkhPTA7CiAgICAgICAgICAgIGlmICh0cmllWyFpZHhdW2N1cl0hPTAmJmNudFt0cmllWyFpZHhdW2N1cl1dIT0wKSB7CiAgICAgICAgICAgICAgICByZXN8PSgxPDxpKTsKICAgICAgICAgICAgICAgIGN1cj10cmllWyFpZHhdW2N1cl07CiAgICAgICAgICAgIH0KICAgICAgICAgICAgZWxzZSBjdXI9dHJpZVtpZHhdW2N1cl07CiAgICAgICAgfQogICAgICAgIHJldHVybiByZXM7CiAgICB9Cn07CgppbnQgbixLOwppbnQgY1tOXSwgYVtOXTsKdmVjdG9yPGludD4gZVtOXTsKaW50IGhzb25bTl0sIHN6W05dOwppbnQgY2xyW05dOwppbnQgY250W04qMzJde307CmludCBhbnNbTl07ClRyaWUgc3VidHJlZSwgcm9vdDsKdm9pZCBkZnMxKGludCB2LGludCBwcmUpewogICAgaW50IG14PTAsc29uPTA7IAogICAgZm9yIChpbnQgaTplW3ZdKSBpZiAoaSE9cHJlKXsKICAgICAgICBkZnMxKGksdik7CiAgICAgICAgaWYgKHN6W2ldPm14KXsKICAgICAgICAgICAgbXg9c3pbaV07CiAgICAgICAgICAgIHNvbj1pOwogICAgICAgIH0KICAgICAgICBzelt2XSs9c3pbaV07CiAgICB9CiAgICBoc29uW3ZdPXNvbjsKICAgIHN6W3ZdKys7Cn0Kdm9pZCBhZGQoaW50IHYpewogICAgcm9vdC5kZWwoY2xyW2Nbdl1dKTsKICAgIGNscltjW3ZdXV49YVt2XTsKICAgIHJvb3QuYWRkKGNscltjW3ZdXSk7CiAgICBzdWJ0cmVlLmFkZChhW3ZdKTsKfQp2b2lkIGRlbChpbnQgdil7CiAgICByb290LmRlbChjbHJbY1t2XV0pOwogICAgY2xyW2Nbdl1dXj1hW3ZdOwogICAgcm9vdC5hZGQoY2xyW2Nbdl1dKTsKICAgIHN1YnRyZWUuZGVsKGFbdl0pOwp9CnZvaWQgRERlbChpbnQgdixpbnQgcHJlKXsKICAgIGZvciAoaW50IGk6ZVt2XSkgaWYgKGkhPXByZSkgRERlbChpLHYpOwogICAgZGVsKHYpOwp9CnZvaWQgQUFkZChpbnQgdixpbnQgcHJlKXsKICAgIGZvciAoaW50IGk6ZVt2XSkgaWYgKGkhPXByZSkgQUFkZChpLHYpOwogICAgYWRkKHYpOwp9CnZvaWQgY2FsY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