CF1778E.The Tree Has Fallen!

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Recently, a tree has fallen on Bob's head from the sky. The tree has nn nodes. Each node uu of the tree has an integer number aua_u written on it. But the tree has no fixed root, as it has fallen from the sky.

Bob is currently studying the tree. To add some twist, Alice proposes a game. First, Bob chooses some node rr to be the root of the tree. After that, Alice chooses a node vv and tells him. Bob then can pick one or more nodes from the subtree of vv . His score will be the bitwise XOR of all the values written on the nodes picked by him. Bob has to find the maximum score he can achieve for the given rr and vv .

As Bob is not a good problem-solver, he asks you to help him find the answer. Can you help him? You need to find the answers for several combinations of rr and vv for the same tree.

Recall that a tree is a connected undirected graph without cycles. The subtree of a node uu is the set of all nodes yy such that the simple path from yy to the root passes through uu . Note that uu is in the subtree of uu .

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt ( 1t1051 \le t \le 10^5 ). The description of the test cases follows.

The first line contains an integer nn ( 2n21052\leq n\leq 2 \cdot 10^5 ) — the number of nodes of the tree.

The next line contains nn space-separated integers a1,a2,,ana_1, a_2, \dots, a_n ( 1ai1091\leq a_i\leq 10^9 ) — the values written on the nodes.

Each of the next n1n-1 lines contain two integers uu and vv ( 1u,vn1\leq u, v\leq n ; uvu\neq v ), denoting there is an undirected edge between node uu and node vv . It is guaranteed that the given edges form a tree.

The next line contains an integer qq ( 1q21051\leq q\leq 2 \cdot 10^5 ) — the number of queries.

Each of the next qq lines contains two integers rr and vv ( 1r,vn1\leq r, v\leq n ) — the root node Bob defines, and the node Alice chooses.

It is guaranteed that the sum of nn and the sum of qq over all test cases don't exceed 21052 \cdot 10^5 .

输出格式

For each test case, in each query, print a line containing an integer denoting the maximum score Bob can achieve.

输入输出样例

  • 输入#1

    3
    6
    12 12 8 25 6 1
    1 5
    1 2
    2 6
    2 3
    2 4
    3
    4 2
    3 5
    1 2
    2
    3 8
    1 2
    4
    2 2
    2 1
    1 2
    1 1
    3
    3 8 7
    1 2
    2 3
    2
    2 2
    2 1

    输出#1

    15
    6
    29
    11
    3
    8
    11
    15
    3

说明/提示

In each of the below figures, the green-colored node is the node picked by Bob, and the red-colored node is the node picked by Alice. The values of the nodes are placed in the blue boxes beside the nodes.

Consider the first example. In the first query, if we put the root node 44 at the top of the tree, the tree looks like the below figure:

Tree with root node 44 in the first query. The nodes in the subtree of the node 22 are [2,1,5,6,3][2,1,5,6,3] . Among them, Bob can pick node 33 , node 55 , and node 66 . His score will be a3a5a6=861=15a_3 \oplus a_5 \oplus a_6 = 8 \oplus 6 \oplus 1 = 15 . He can't achieve any score more than this.In the second query, if the root node 33 is placed at the top of the tree, the tree looks like the below figure:

Tree with root node 33 in the second query. The only node in the subtree of the node 55 is 55 . Bob can only pick the node 55 . His score will be a5=6a_5 = 6 .In the third query, if the root node 11 is placed at the top of the tree, the tree looks like this:

Tree with root node 11 in the third query. The nodes in the subtree of the node 22 are [2,3,6,4][2,3,6,4] . Among them, Bob can pick node 22 , node 33 , and node 44 . His score will be a2a3a4=12825=29a_2 \oplus a_3 \oplus a_4 = 12 \oplus 8 \oplus 25 = 29 . He is not able to score higher than that.

首页