CF1778E.The Tree Has Fallen!

省选/NOI-

通过率:0%

时间限制:3.00s

内存限制:512MB

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.

最近,一棵树从天而降,砸在了 Bob 的头上。这棵树有 nn 个节点。树中每个节点 uu 上都写有一个整数 aua_u。但由于这棵树是从天上掉下来的,因此它没有固定的根节点。

Bob 正在研究这棵树。为了增添一些趣味性,Alice 提出了一个游戏:首先,Bob 任选一个节点 rr 作为树的根;接着,Alice 选定一个节点 vv 并告诉 Bob;然后,Bob 可以从节点 vv 的子树中选取一个或多个节点;他的得分为所选节点上所有数值的按位异或(XOR)(参见按位运算#异或)。对于给定的 rrvv,Bob 需要找出他所能获得的最大得分。

由于 Bob 不擅长解题,他请你帮忙找出答案。你能帮他吗?你需要对同一棵树的多组 (r,v)(r, v) 组合分别求出对应答案。

注意:树是一种无环的连通无向图。节点 uu 的子树定义为所有满足“从 yy 到根节点的简单路径经过 uu”的节点 yy 所构成的集合。特别地,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.

每个测试包含多个测试用例。第一行包含测试用例数量 tt1t1051 \le t \le 10^5)。随后是各测试用例的描述。

第一行包含一个整数 nn2n21052\leq n\leq 2 \cdot 10^5)——树中节点的数量。

第二行包含 nn 个以空格分隔的整数 a1,a2,,ana_1, a_2, \dots, a_n1ai1091\leq a_i\leq 10^9)——写在各节点上的数值。

接下来的 n1n-1 行,每行包含两个整数 uuvv1u,vn1\leq u, v\leq nuvu\neq v),表示节点 uu 与节点 vv 之间存在一条无向边。保证所给边构成一棵树。

接下来一行包含一个整数 qq1q21051\leq q\leq 2 \cdot 10^5)——查询的数量。

接下来的 qq 行,每行包含两个整数 rrvv1r,vn1\leq r, v\leq n)——Bob 所指定的根节点,以及 Alice 所选择的节点。

保证所有测试用例中 nn 的总和与 qq 的总和均不超过 21052 \cdot 10^5

输出格式

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

对于每个测试用例,在每次查询中,输出一行,包含一个整数,表示 Bob 能够获得的最大得分。

输入输出样例

  • 输入#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.

在以下每个图中,绿色节点表示 Bob 选择的节点,红色节点表示 Alice 选择的节点。各节点的值显示在节点旁的蓝色方框中。

考虑第一个例子。在第一次查询中,若将根节点 44 置于树的顶部,则树的结构如下图所示:

第一次查询中以节点 44 为根的树。

节点 22 的子树中包含节点 [2,1,5,6,3][2,1,5,6,3]。其中,Bob 可选择节点 33、节点 55 和节点 66。他的得分为 a3a5a6=861=15a_3 \oplus a_5 \oplus a_6 = 8 \oplus 6 \oplus 1 = 15。他无法获得高于此值的得分。

在第二次查询中,若将根节点 33 置于树的顶部,则树的结构如下图所示:

第二次查询中以节点 33 为根的树。

节点 55 的子树中仅包含节点 55。Bob 只能选择节点 55,其得分为 a5=6a_5 = 6

在第三次查询中,若将根节点 11 置于树的顶部,则树的结构如下图所示:

第三次查询中以节点 11 为根的树。

节点 22 的子树中包含节点 [2,3,6,4][2,3,6,4]。其中,Bob 可选择节点 22、节点 33 和节点 44。他的得分为 a2a3a4=12825=29a_2 \oplus a_3 \oplus a_4 = 12 \oplus 8 \oplus 25 = 29。他无法获得高于此值的得分。

输入解题思路,AI测评打分。不知道怎么写?

首页