CF1878G.wxhtzdy ORO Tree

提高+/省选-

通过率:0%

时间限制:5.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

After (finally) qualifying for the IOI 2023, wxhtzdy was very happy, so he decided to do what most competitive programmers do: trying to guess the problems that will be on IOI. During this process, he accidentally made a problem, which he thought was really cool.

You are given a tree (a connected acyclic graph) with nn vertices and n−1n-1 edges. Vertex ii (1≤i≤n1 \le i \le n) has a value aia_i.

Lets' define g(u,v)g(u, v) as the bitwise or of the values of all vertices on the shortest path from uu to vv. For example, let's say that we want to calculate g(3,4)g(3, 4), on the tree from the first test case in the example. On the path from 33 to 44 are vertices 33, 11, 44. Then, g(3,4)=a3 ∣ a1 ∣ a4g(3, 4) = a_3 \ | \ a_1 \ | \ a_4 (here, ∣| represents the bitwise OR operation).

Also, you are given qq queries, and each query looks like this:

You are given xx and yy. Let's consider all vertices zz such that zz is on the shortest path from xx to yy (inclusive).

Lets define the niceness of a vertex zz as the sum of the number of non-zero bits in g(x,z)g(x, z) and the number of non-zero bits in g(y,z)g(y, z). You need to find the maximum niceness among all vertices zz on the shortest path from xx to yy.

Since his brain is really tired after solving an output only problem on SIO (he had to do it to qualify for the IOI), he wants your help with this problem.

在(终于)获得 IOI 2023 参赛资格后,wxhtzdy 非常开心,于是决定做大多数竞赛程序员都会做的事:尝试猜测 IOI 上可能出现的题目。在此过程中,他意外地构造出了一道题,并认为这道题非常酷。

你将得到一棵树(一个连通无环图),该树包含 nn 个顶点和 n−1n-1 条边。顶点 ii(1≤i≤n1 \le i \le n)具有一个权值 aia_i。

我们定义 g(u,v)g(u, v) 为从 uu 到 vv 的最短路径上所有顶点的权值的按位或(bitwise OR)。例如,在样例的第一个测试用例所给的树中,若要计算 g(3,4)g(3, 4),则从 33 到 44 的路径上的顶点依次为 33、11、44,因此 g(3,4)=a3 ∣ a1 ∣ a4g(3, 4) = a_3 \ | \ a_1 \ | \ a_4(此处 ∣| 表示按位或运算)。

此外,你还会得到 qq 个查询,每个查询的形式如下:

给定 xx 和 yy。考虑所有位于从 xx 到 yy 的最短路径上的顶点 zz(含端点)。

我们定义顶点 zz 的**优美度(niceness)**为:g(x,z)g(x, z) 的二进制表示中非零位的个数,加上 g(y,z)g(y, z) 的二进制表示中非零位的个数。你需要找出所有满足条件的 zz(即所有在 xx 到 yy 最短路径上的顶点)中,最大的优美度。

由于他在 SIO 上刚解完一道“仅输出”类题目(他必须完成该题才能获得 IOI 参赛资格),大脑已极度疲惫,因此希望你能帮他解决这个问题。

输入格式

The first line of input contains a single integer tt (1≤t≤1041 \le t \le 10^4) — the number of test cases.

The first line of each test case contains a single integer nn (1≤n≤2⋅1051 \le n \le 2 \cdot 10^5) — the number of vertices.

The second line of each test case contains nn positive integers a1,a2,…,ana_1, a_2, \dots, a_n (1≤av≤1091 \le a_v \le 10^9) — the value of each vertex, the ii-th integer in this line corresponds to the vertex ii.

Following n−1n - 1 lines are the description of a tree.

Each line contains two integers uu and vv (1≤u,v≤n,u≠v1 \le u, v \le n, u \ne v) — indicating that vertices uu and vv are connected by an edge.

The next line contains a single integer qq (1≤q≤1051 \le q \le 10^5) — number of queries.

Following qq lines contain 2 integers x,yx, y (1≤x,y≤n1 \le x, y \le n) — the vertices xx and yy for each query.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052 \cdot 10^5.

It is guaranteed that the sum of qq over all test cases does not exceed 10510^5.

输入的第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4)——测试用例的数量。

每个测试用例的第一行包含一个整数 nn(1≤n≤2⋅1051 \le n \le 2 \cdot 10^5)——顶点的数量。

每个测试用例的第二行包含 nn 个正整数 a1,a2,…,ana_1, a_2, \dots, a_n(1≤av≤1091 \le a_v \le 10^9)——每个顶点的权值,该行中第 ii 个整数对应顶点 ii。

接下来的 n−1n - 1 行描述一棵树。

每行包含两个整数 uu 和 vv(1≤u,v≤n, u≠v1 \le u, v \le n,\ u \ne v)——表示顶点 uu 和 vv 之间有一条边相连。

下一行包含一个整数 qq(1≤q≤1051 \le q \le 10^5)——查询的数量。

接下来的 qq 行每行包含两个整数 x,yx, y(1≤x,y≤n1 \le x, y \le n)——表示每次查询所涉及的顶点 xx 和 yy。

保证所有测试用例的 nn 之和不超过 2⋅1052 \cdot 10^5。

保证所有测试用例的 qq 之和不超过 10510^5。

输出格式

For each test case output qq integers, each of which is the answer to the corresponding query.

对每个测试用例,输出 qq 个整数,每个整数对应相应查询的答案。

输入输出样例

  • 输入#1

    3
    4
    1 2 3 4
    1 3
    1 2
    1 4
    3
    1 1
    1 3
    1 4
    3
    7 6 3
    3 1
    2 1
    4
    1 1
    1 2
    1 3
    2 3
    1
    4
    1
    1 1

    输出#1

    2 4 3 
    6 6 6 6 
    2
  • 输入#2

    3
    7
    4 7 7 4 10 8 10
    6 1
    3 1
    2 1
    7 4
    1 5
    4 2
    4
    7 5
    2 3
    4 5
    2 5
    6
    9 5 6 2 4 6
    5 1
    2 1
    1 6
    4 3
    1 3
    4
    6 1
    1 4
    4 3
    3 5
    7
    5 1 3 7 5 1 6
    2 1
    5 4
    2 3
    3 4
    7 6
    6 3
    2
    4 2
    7 7

    输出#2

    8 6 7 7 
    6 6 4 7 
    6 4
  • 输入#3

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

    输出#3

    7 7 5 7

说明/提示

The image below shows the tree from the second example, first test case.

Tree from the second example, first test case

In the first query, we have x=7x=7, y=5y=5. The shortest path from 77 to 55 is 7−4−2−1−57-4-2-1-5.

Let's calculate the niceness of vertex 77 on this path. We have g(7,7)=a7=10=(1010)2g(7,7)=a_7=10=(1010)_2 and g(5,7)=a5 ∣ a1 ∣ a2 ∣ a4 ∣ a7=10 ∣ 4 ∣ 7 ∣ 4 ∣ 10=15=(1111)2g(5,7)=a_5 \ | \ a_1 \ | \ a_2 \ | \ a_4 \ | \ a_7=10 \ | \ 4 \ | \ 7 \ | \ 4 \ | \ 10=15=(1111)_2, so its niceness is equal to 2+4=62 + 4 = 6.

Now let's calculate the niceness of vertex 44 on this path. We have g(7,4)=a7 ∣ a4=10 ∣ 4=14=(1110)2g(7,4)=a_7 \ | \ a_4=10 \ | \ 4=14=(1110)_2 and g(5,4)=a5 ∣ a1 ∣ a2 ∣ a4=10 ∣ 4 ∣ 7 ∣ 4=15=(1111)2g(5,4)=a_5 \ | \ a_1 \ | \ a_2 \ | \ a_4=10 \ | \ 4 \ | \ 7 \ | \ 4=15=(1111)_2, so its niceness is equal to 3+4=73 + 4 = 7.

Now let's calculate the niceness of vertex 22 on this path. We have g(7,2)=a7 ∣ a4 ∣ a2=10 ∣ 4 ∣ 7=15=(1111)2g(7,2)=a_7 \ | \ a_4 \ | \ a_2=10 \ | \ 4 \ | \ 7=15=(1111)_2 and g(5,2)=a5 ∣ a1 ∣ a2=10 ∣ 4 ∣ 7=15=(1111)2g(5,2)=a_5 \ | \ a_1 \ | \ a_2=10 \ | \ 4 \ | \ 7=15=(1111)_2, so its niceness is equal to 4+4=84 + 4 = 8.

Now let's calculate the niceness of vertex 11 on this path. We have g(7,1)=a7 ∣ a4 ∣ a2 ∣ a1=10 ∣ 4 ∣ 7 ∣ 4=15=(1111)2g(7,1)=a_7 \ | \ a_4 \ | \ a_2 \ | \ a_1=10 \ | \ 4 \ | \ 7 \ | \ 4=15=(1111)_2 and g(5,1)=a5 ∣ a1=10 ∣ 4=14=(1110)2g(5,1)=a_5 \ | \ a_1=10 \ | \ 4=14=(1110)_2, so its niceness is equal to 4+3=74 + 3 = 7.

Finally, let's calculate the niceness of vertex 55 on this path. We have g(7,5)=a7 ∣ a4 ∣ a2 ∣ a1 ∣ a5=10 ∣ 4 ∣ 7 ∣ 4 ∣ 10=15=(1111)2g(7,5)=a_7 \ | \ a_4 \ | \ a_2 \ | \ a_1 \ | \ a_5=10 \ | \ 4 \ | \ 7 \ | \ 4 \ | \ 10=15=(1111)_2 and g(5,5)=a5=10=(1010)2g(5,5)=a_5=10=(1010)_2, so its niceness is equal to 4+2=64 + 2 = 6.

The maximum niceness on this path is at vertex 22, and it is 88.

下图展示了第二个样例的第一组测试用例中的树。

第二个样例,第一组测试用例中的树

在第一个查询中,我们有 x=7x=7,y=5y=5。从 77 到 55 的最短路径为 7−4−2−1−57-4-2-1-5。

我们来计算该路径上顶点 77 的“优美度”(niceness)。我们有 g(7,7)=a7=10=(1010)2g(7,7)=a_7=10=(1010)_2,且 g(5,7)=a5 ∣ a1 ∣ a2 ∣ a4 ∣ a7=10 ∣ 4 ∣ 7 ∣ 4 ∣ 10=15=(1111)2g(5,7)=a_5 \ | \ a_1 \ | \ a_2 \ | \ a_4 \ | \ a_7=10 \ | \ 4 \ | \ 7 \ | \ 4 \ | \ 10=15=(1111)_2,因此其优美度为 2+4=62 + 4 = 6。

接下来计算该路径上顶点 44 的优美度。我们有 g(7,4)=a7 ∣ a4=10 ∣ 4=14=(1110)2g(7,4)=a_7 \ | \ a_4=10 \ | \ 4=14=(1110)_2,且 g(5,4)=a5 ∣ a1 ∣ a2 ∣ a4=10 ∣ 4 ∣ 7 ∣ 4=15=(1111)2g(5,4)=a_5 \ | \ a_1 \ | \ a_2 \ | \ a_4=10 \ | \ 4 \ | \ 7 \ | \ 4=15=(1111)_2,因此其优美度为 3+4=73 + 4 = 7。

接下来计算该路径上顶点 22 的优美度。我们有 g(7,2)=a7 ∣ a4 ∣ a2=10 ∣ 4 ∣ 7=15=(1111)2g(7,2)=a_7 \ | \ a_4 \ | \ a_2=10 \ | \ 4 \ | \ 7=15=(1111)_2,且 g(5,2)=a5 ∣ a1 ∣ a2=10 ∣ 4 ∣ 7=15=(1111)2g(5,2)=a_5 \ | \ a_1 \ | \ a_2=10 \ | \ 4 \ | \ 7=15=(1111)_2,因此其优美度为 4+4=84 + 4 = 8。

接下来计算该路径上顶点 11 的优美度。我们有 g(7,1)=a7 ∣ a4 ∣ a2 ∣ a1=10 ∣ 4 ∣ 7 ∣ 4=15=(1111)2g(7,1)=a_7 \ | \ a_4 \ | \ a_2 \ | \ a_1=10 \ | \ 4 \ | \ 7 \ | \ 4=15=(1111)_2,且 g(5,1)=a5 ∣ a1=10 ∣ 4=14=(1110)2g(5,1)=a_5 \ | \ a_1=10 \ | \ 4=14=(1110)_2,因此其优美度为 4+3=74 + 3 = 7。

最后,计算该路径上顶点 55 的优美度。我们有 g(7,5)=a7 ∣ a4 ∣ a2 ∣ a1 ∣ a5=10 ∣ 4 ∣ 7 ∣ 4 ∣ 10=15=(1111)2g(7,5)=a_7 \ | \ a_4 \ | \ a_2 \ | \ a_1 \ | \ a_5=10 \ | \ 4 \ | \ 7 \ | \ 4 \ | \ 10=15=(1111)_2,且 g(5,5)=a5=10=(1010)2g(5,5)=a_5=10=(1010)_2,因此其优美度为 4+2=64 + 2 = 6。

该路径上的最大优美度出现在顶点 22 处,其值为 88。

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

首页