luogu#P16610. [Algo Beat 008 & WWOI R3] 银河战区

[Algo Beat 008 & WWOI R3] 银河战区

Problem Description

How wonderful! The starting point and the destination cannot be decided, and the game ends halfway. The light of the end shines upon the earth, and students in the school begin to go to the cafeteria to buy hamburgers.

You have nn hamburgers, where each hamburger ii has a deliciousness value aia_i. You can place these hamburgers arbitrarily on the nn nodes of a tree, such that each node contains exactly one hamburger. The hamburgers at school are so delicious, so we define the total deliciousness as follows: for every chain on the tree leading to the root node 11, calculate the XOR sum of the deliciousness values of the hamburgers on that chain. The total deliciousness is the bitwise AND sum of all these XOR results.

What is the maximum total deliciousness you can obtain after placing the hamburgers on the tree in an optimal way?

I suddenly... really want... to eat... a hamburger...

Original Formal Problem Statement:

There is a tree with nn nodes, numbered from 11 to nn, with the root node being 11; and a sequence aa of length nn.

You can arbitrarily rearrange the sequence aa, and then sequentially assign the node weight of node ii on the tree to be aia_i, in order to maximize the value of this tree.

We define f(i)f(i) as the XOR sum of the node weights (including the node itself) on the simple path from node ii to the root of the tree. Then, the value of the tree is defined as: ANDi=1nf(i)\operatorname{AND}_{i=1}^{n}f(i). That is, the bitwise AND sum of f(i)f(i) for nodes 11 to nn.

Input Format

The first line contains a positive integer nn.

The next line contains nn positive integers representing the sequence aa.

The following n1n-1 lines each contain two integers uu and vv, indicating that there is an edge between node uu and node vv. It is guaranteed that the graph is a tree.

Output Format

Output a single non-negative integer on one line, representing the maximum value of the tree.

5
2 4 4 4 4
1 2
2 3
3 4
4 5
2

Hint

This problem uses bundled testing.

For all test data, the following conditions apply:

  • 1u,vn1051 \leq u,v \leq n \leq 10^5.
  • 1ai1091 \leq a_i \leq 10^9.
Subtask Number nn\leq Special Properties Score
11 99 None 2020
22 1515 ^
33 10310^3 3030
44 10510^5 Data is randomly generated, and n104n\geq 10^4 11
55 None 2929

Translated by Qwen 3.6