#include <bits/stdc++.h>
#define fi first
#define se second
#define all(v) v.begin() , v.end()
#define sz(v) int(v.size())
#define unq(v) sort(all(v)); v.resize(unique(all(v)) - v.begin());
using namespace std;
typedef long long ll;
typedef pair<int , int> ii;
typedef pair<long long , int> lli;
const int maxN = int(2e5)+7;
const int LOG = 30;
int n , a[maxN] , d[maxN] , p[maxN] , sz[maxN];
vector<int> g[maxN];
void dfs_init(int u){
d[u] = d[p[u]] ^ a[u];
sz[u] = 1;
for (int v : g[u]){
dfs_init(v);
sz[u] += sz[v];
}
}
struct node{
int nxt[2] , cnt;
node(){
cnt = 0;
nxt[0] = nxt[1] = -1;
}
};
vector<node> trie;
int nxt_id(int id , int c){
if (id == -1) return -1; else return trie[id].nxt[c];
}
int get_cnt(int id){
if (id == -1) return 0; else return trie[id].cnt;
}
void update(int x , int y){
int id = 0;
for (int i = LOG ; i >= 0 ; i--){
int c = (x>>i)&1;
if (trie[id].nxt[c] == -1){
trie.push_back(node());
trie[id].nxt[c] = sz(trie) - 1;
}
id = trie[id].nxt[c];
trie[id].cnt += y;
}
}
int get(int x){
int id = 0 , ans = 0;
if (get_cnt(nxt_id(id , 0)) + get_cnt(nxt_id(id , 1)) == 0) return 0;
for (int i = LOG ; i >= 0 ; i--){
int c = 1 - ((x>>i)&1);
if (get_cnt(nxt_id(id , c)) == 0){
c ^= 1;
}
else{
ans ^= (1<<i);
}
id = nxt_id(id , c);
}
return ans;
}
int dp[maxN];
void dfs_update(int u , int x){
update(d[u] , x);
for (int v : g[u]){
dfs_update(v , x);
}
}
void dfs_get(int u , int x){
dp[x] = max(dp[x] , get(d[u] ^ a[x]));
for (int v : g[u]){
dfs_get(v , x);
}
}
void dfs_solve(int u){
dp[u] = a[u];
int heavy = -1;
for (int v : g[u]){
if (heavy == -1 || sz[heavy] < sz[v]) heavy = v;
}
for (int v : g[u]){
if (v != heavy){
dfs_solve(v);
dfs_update(v , -1);
}
}
if (heavy != -1) dfs_solve(heavy);
for (int v : g[u]){
if (v != heavy){
dfs_get(v , u);
dfs_update(v , +1);
}
}
dp[u] = max(dp[u] , get(d[u] ^ a[u]));
update(d[u] , +1);
for (int v : g[u]){
dp[u] = max(dp[u] , dp[v]);
}
}
void solve(){
cin >> n;
trie.push_back(node());
for (int i = 1 ; i <= n ; i++){
cin >> a[i];
}
for (int i = 2 ; i <= n ; i++){
cin >> p[i];
g[p[i]].push_back(i);
}
dfs_init(1);
for (int i = 1 ; i <= n ; i++) update(d[i] , 0);
dfs_solve(1);
for (int i = 1 ; i <= n ; i++) cout << dp[i] << "\n";
}
#define name "H"
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if (fopen(name".INP" , "r")){
freopen(name".INP" , "r" , stdin);
freopen(name".OUT" , "w" , stdout);
}
int t = 1; //cin >> t;
while (t--) solve();
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNkZWZpbmUgZmkgZmlyc3QKI2RlZmluZSBzZSBzZWNvbmQKI2RlZmluZSBhbGwodikgdi5iZWdpbigpICwgdi5lbmQoKQojZGVmaW5lIHN6KHYpIGludCh2LnNpemUoKSkKI2RlZmluZSB1bnEodikgc29ydChhbGwodikpOyB2LnJlc2l6ZSh1bmlxdWUoYWxsKHYpKSAtIHYuYmVnaW4oKSk7CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgp0eXBlZGVmIGxvbmcgbG9uZyBsbDsKdHlwZWRlZiBwYWlyPGludCAsIGludD4gaWk7CnR5cGVkZWYgcGFpcjxsb25nIGxvbmcgLCBpbnQ+IGxsaTsKCmNvbnN0IGludCBtYXhOID0gaW50KDJlNSkrNzsKY29uc3QgaW50IExPRyA9IDMwOwoKaW50IG4gLCBhW21heE5dICwgZFttYXhOXSAsIHBbbWF4Tl0gLCBzelttYXhOXTsKdmVjdG9yPGludD4gZ1ttYXhOXTsKCnZvaWQgZGZzX2luaXQoaW50IHUpewogICAgZFt1XSA9IGRbcFt1XV0gXiBhW3VdOwogICAgc3pbdV0gPSAxOwogICAgZm9yIChpbnQgdiA6IGdbdV0pewogICAgICAgIGRmc19pbml0KHYpOwogICAgICAgIHN6W3VdICs9IHN6W3ZdOwogICAgfQp9CgpzdHJ1Y3Qgbm9kZXsKICAgIGludCBueHRbMl0gLCBjbnQ7CgogICAgbm9kZSgpewogICAgICAgIGNudCA9IDA7CiAgICAgICAgbnh0WzBdID0gbnh0WzFdID0gLTE7CiAgICB9Cn07Cgp2ZWN0b3I8bm9kZT4gdHJpZTsKCmludCBueHRfaWQoaW50IGlkICwgaW50IGMpewogICAgaWYgKGlkID09IC0xKSByZXR1cm4gLTE7IGVsc2UgcmV0dXJuIHRyaWVbaWRdLm54dFtjXTsKfQoKaW50IGdldF9jbnQoaW50IGlkKXsKICAgIGlmIChpZCA9PSAtMSkgcmV0dXJuIDA7IGVsc2UgcmV0dXJuIHRyaWVbaWRdLmNudDsKfQoKdm9pZCB1cGRhdGUoaW50IHggLCBpbnQgeSl7CiAgICBpbnQgaWQgPSAwOwogICAgZm9yIChpbnQgaSA9IExPRyA7IGkgPj0gMCA7IGktLSl7CiAgICAgICAgaW50IGMgPSAoeD4+aSkmMTsKICAgICAgICBpZiAodHJpZVtpZF0ubnh0W2NdID09IC0xKXsKICAgICAgICAgICAgdHJpZS5wdXNoX2JhY2sobm9kZSgpKTsKICAgICAgICAgICAgdHJpZVtpZF0ubnh0W2NdID0gc3oodHJpZSkgLSAxOwogICAgICAgIH0KICAgICAgICBpZCA9IHRyaWVbaWRdLm54dFtjXTsKICAgICAgICB0cmllW2lkXS5jbnQgKz0geTsKICAgIH0KfQoKaW50IGdldChpbnQgeCl7CiAgICBpbnQgaWQgPSAwICwgYW5zID0gMDsKICAgIGlmIChnZXRfY250KG54dF9pZChpZCAsIDApKSArIGdldF9jbnQobnh0X2lkKGlkICwgMSkpID09IDApIHJldHVybiAwOwogICAgZm9yIChpbnQgaSA9IExPRyA7IGkgPj0gMCA7IGktLSl7CiAgICAgICAgaW50IGMgPSAxIC0gKCh4Pj5pKSYxKTsKICAgICAgICBpZiAoZ2V0X2NudChueHRfaWQoaWQgLCBjKSkgPT0gMCl7CiAgICAgICAgICAgIGMgXj0gMTsKICAgICAgICB9CiAgICAgICAgZWxzZXsKICAgICAgICAgICAgYW5zIF49ICgxPDxpKTsKICAgICAgICB9CiAgICAgICAgaWQgPSBueHRfaWQoaWQgLCBjKTsKICAgIH0KICAgIHJldHVybiBhbnM7Cn0KCmludCBkcFttYXhOXTsKCnZvaWQgZGZzX3VwZGF0ZShpbnQgdSAsIGludCB4KXsKICAgIHVwZGF0ZShkW3VdICwgeCk7CiAgICBmb3IgKGludCB2IDogZ1t1XSl7CiAgICAgICAgZGZzX3VwZGF0ZSh2ICwgeCk7CiAgICB9Cn0KCnZvaWQgZGZzX2dldChpbnQgdSAsIGludCB4KXsKICAgIGRwW3hdID0gbWF4KGRwW3hdICwgZ2V0KGRbdV0gXiBhW3hdKSk7CiAgICBmb3IgKGludCB2IDogZ1t1XSl7CiAgICAgICAgZGZzX2dldCh2ICwgeCk7CiAgICB9Cn0KCnZvaWQgZGZzX3NvbHZlKGludCB1KXsKICAgIGRwW3VdID0gYVt1XTsKICAgIGludCBoZWF2eSA9IC0xOwogICAgZm9yIChpbnQgdiA6IGdbdV0pewogICAgICAgIGlmIChoZWF2eSA9PSAtMSB8fCBzeltoZWF2eV0gPCBzelt2XSkgaGVhdnkgPSB2OwogICAgfQogICAgZm9yIChpbnQgdiA6IGdbdV0pewogICAgICAgIGlmICh2ICE9IGhlYXZ5KXsKICAgICAgICAgICAgZGZzX3NvbHZlKHYpOwogICAgICAgICAgICBkZnNfdXBkYXRlKHYgLCAtMSk7CiAgICAgICAgfQogICAgfQogICAgaWYgKGhlYXZ5ICE9IC0xKSBkZnNfc29sdmUoaGVhdnkpOwogICAgZm9yIChpbnQgdiA6IGdbdV0pewogICAgICAgIGlmICh2ICE9IGhlYXZ5KXsKICAgICAgICAgICAgZGZzX2dldCh2ICwgdSk7CiAgICAgICAgICAgIGRmc191cGRhd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