#P8511. [Ynoi Easy Round 2021] TEST_68

[Ynoi Easy Round 2021] TEST_68

Problem Description

Given a tree with nn nodes, node ii has a weight aia_i.

For each node xx, its answer is defined as follows: among all nodes outside the subtree of xx, choose two nodes i,ji, j (they may be the same node), and maximize aia_i xor aja_j. If it is impossible to choose two nodes, then the answer for xx is 00.

Input Format

The first line contains an integer nn.

The next line contains n1n-1 integers. The ii-th integer denotes the parent node jj of node i+1i+1, and it is guaranteed that j<i+1j < i+1.

The next line contains nn integers. The ii-th integer denotes the weight aia_i of node ii.

Output Format

Output nn lines, each containing one integer. The integer on the ii-th line is the answer for node ii.

10
1 1 2 3 2 3 6 7 7
10 6 4 10 8 10 5 3 5 4
0
15
12
15
15
15
14
15
15
15

Hint

Idea: nzhtl1477, Solution: zx2003, Code: nzhtl1477, Data: nzhtl1477.

For 10%10\% of the testdata, 1n1021 \le n \le 10^2.

For another 20%20\% of the testdata, 1n1041 \le n \le 10^4.

For another 30%30\% of the testdata, the tree is a chain.

For another 20%20\% of the testdata, 0ai1020 \le a_i \le 10^2.

For 100%100\% of the testdata, 1n5×1051 \le n \le 5 \times 10^5 and 0ai10180 \le a_i \le 10^{18}.

Translated by ChatGPT 5