Under construction

Still being built. Things may change

Orange Farm Simulation

Time limit10sMemory limit1024 MB

Summary
For each road in a tree of fields, find the largest XOR of competitiveness values between a field on one side and a field on the other side after cutting that road.
Level

Hard8 of 10

Topics
Tree, Trie, Bit manipulation, DFS
Solved
No attempts yet

Problem

Bru is playing an orange farm simulation game. After playing for a long time, Bru has built NN orange fields on the farm, and the fields are connected by N−1N-1 roads so that every field can be reached from every other field.

As the farm grows, Bru wants to cut one road to split the farm in two. Splitting the farm lets both halves grow at the same time, so efficiency goes up. However, once the farm is split, the fields on opposite sides enter a competitive relationship. Field ii has competitiveness AiA_i. If field ii and field jj are in a competitive relationship, the competition index between them is Ai⊕AjA_i \oplus A_j, where ⊕\oplus is bitwise XOR. The total competition index for cutting one road is the maximum competition index over all competitive pairs created by that cut.

For example, take the farm in the figure above with Ai=iA_i = i, and cut the road between fields 2 and 3, as in the figure below.

The competition indices of all competitive pairs are:

  • Field 3 with fields 1, 2, 4, and 7: 2, 1, 7, 4
  • Field 5 with fields 1, 2, 4, and 7: 4, 7, 1, 2
  • Field 6 with fields 1, 2, 4, and 7: 7, 4, 2, 1

The maximum is 7, so the total competition index for cutting the road between fields 2 and 3 is 7.

Write a program that computes the total competition index for cutting each road.

Input

The first line contains the number of fields NN.

The second line contains the competitiveness of each field, A1,A2,⋯ ,ANA_1, A_2, \cdots, A_N.

From the third line to line N+1N+1, line i+2i+2 contains the numbers of the two fields uiu_i and viv_i joined by road ii, in the form ui viu_i \ v_i.

Output

Print the total competition index for cutting each road, from road 1 to road N−1N-1, one per line, in order.

Constraints

  • 2≤N≤2×1052 \le N \le 2 \times 10^5
  • 0≤Ai<2200 \le A_i < 2^{20}
  • 1≤ui,vi≤N1 \le u_i, v_i \le N
  • ui≠viu_i \ne v_i
  • Every field can be reached from every other field through the given roads.

Examples1

  1. Example 1

    Input
    7
    1 2 3 4 5 6 7
    1 2
    2 3
    2 4
    3 5
    3 6
    4 7
    
    Expected output
    7
    7
    7
    7
    7
    6