A16198 | wxhtzdy ORO Tree
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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 $n$ vertices and $n-1$ edges. Vertex $i$ ( $1 \le i \le n$ ) has a value $a_i$ .
Lets' define $g(u, v)$ as the [bitwise or](http://tiny.cc/bitwise_or) of the values of all vertices on the shortest path from $u$ to $v$ . For example, let's say that we want to calculate $g(3, 4)$ , on the tree from the first test case in the example. On the path from $3$ to $4$ are vertices $3$ , $1$ , $4$ . Then, $g(3, 4) = a_3 \ | \ a_1 \ | \ a_4$ (here, $|$ represents the [bitwise OR operation](http://tiny.cc/bitwise_or)).
Also, you are given $q$ queries, and each query looks like this:
You are given $x$ and $y$ . Let's consider all vertices $z$ such that $z$ is on the shortest path from $x$ to $y$ (inclusive).
Lets define the niceness of a vertex $z$ as the sum of the number of non-zero bits in $g(x, z)$ and the number of non-zero bits in $g(y, z)$ . You need to find the maximum niceness among all vertices $z$ on the shortest path from $x$ to $y$ .
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.
You are given a tree (a connected acyclic graph) with $n$ vertices and $n-1$ edges. Vertex $i$ ( $1 \le i \le n$ ) has a value $a_i$ .
Lets' define $g(u, v)$ as the [bitwise or](http://tiny.cc/bitwise_or) of the values of all vertices on the shortest path from $u$ to $v$ . For example, let's say that we want to calculate $g(3, 4)$ , on the tree from the first test case in the example. On the path from $3$ to $4$ are vertices $3$ , $1$ , $4$ . Then, $g(3, 4) = a_3 \ | \ a_1 \ | \ a_4$ (here, $|$ represents the [bitwise OR operation](http://tiny.cc/bitwise_or)).
Also, you are given $q$ queries, and each query looks like this:
You are given $x$ and $y$ . Let's consider all vertices $z$ such that $z$ is on the shortest path from $x$ to $y$ (inclusive).
Lets define the niceness of a vertex $z$ as the sum of the number of non-zero bits in $g(x, z)$ and the number of non-zero bits in $g(y, z)$ . You need to find the maximum niceness among all vertices $z$ on the shortest path from $x$ to $y$ .
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.
输入格式
The first line of input contains a single integer $t$ ( $1 \le t \le 10^4$ ) — the number of test cases.
The first line of each test case contains a single integer $n$ ( $1 \le n \le 2 \cdot 10^5$ ) — the number of vertices.
The second line of each test case contains $n$ positive integers $a_1, a_2, \dots, a_n$ ( $1 \le a_v \le 10^9$ ) — the value of each vertex, the $i$ -th integer in this line corresponds to the vertex $i$ .
Following $n - 1$ lines are the description of a tree.
Each line contains two integers $u$ and $v$ ( $1 \le u, v \le n, u \ne v$ ) — indicating that vertices $u$ and $v$ are connected by an edge.
The next line contains a single integer $q$ ( $1 \le q \le 10^5$ ) — number of queries.
Following $q$ lines contain 2 integers $x, y$ ( $1 \le x, y \le n$ ) — the vertices $x$ and $y$ for each query.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ .
It is guaranteed that the sum of $q$ over all test cases does not exceed $10^5$ .
The first line of each test case contains a single integer $n$ ( $1 \le n \le 2 \cdot 10^5$ ) — the number of vertices.
The second line of each test case contains $n$ positive integers $a_1, a_2, \dots, a_n$ ( $1 \le a_v \le 10^9$ ) — the value of each vertex, the $i$ -th integer in this line corresponds to the vertex $i$ .
Following $n - 1$ lines are the description of a tree.
Each line contains two integers $u$ and $v$ ( $1 \le u, v \le n, u \ne v$ ) — indicating that vertices $u$ and $v$ are connected by an edge.
The next line contains a single integer $q$ ( $1 \le q \le 10^5$ ) — number of queries.
Following $q$ lines contain 2 integers $x, y$ ( $1 \le x, y \le n$ ) — the vertices $x$ and $y$ for each query.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ .
It is guaranteed that the sum of $q$ over all test cases does not exceed $10^5$ .
输出格式
For each test case output $q$ integers, each of which is the answer to the corresponding query.
输入输出样例
输入 #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 caseIn the first query, we have $x=7$ , $y=5$ . The shortest path from $7$ to $5$ is $7-4-2-1-5$ .
Let's calculate the niceness of vertex $7$ on this path. We have $g(7,7)=a_7=10=(1010)_2$ and $g(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 = 6$ .
Now let's calculate the niceness of vertex $4$ on this path. We have $g(7,4)=a_7 \ | \ a_4=10 \ | \ 4=14=(1110)_2$ and $g(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 = 7$ .
Now let's calculate the niceness of vertex $2$ on this path. We have $g(7,2)=a_7 \ | \ a_4 \ | \ a_2=10 \ | \ 4 \ | \ 7=15=(1111)_2$ and $g(5,2)=a_5 \ | \ a_1 \ | \ a_2=10 \ | \ 4 \ | \ 7=15=(1111)_2$ , so its niceness is equal to $4 + 4 = 8$ .
Now let's calculate the niceness of vertex $1$ on this path. We have $g(7,1)=a_7 \ | \ a_4 \ | \ a_2 \ | \ a_1=10 \ | \ 4 \ | \ 7 \ | \ 4=15=(1111)_2$ and $g(5,1)=a_5 \ | \ a_1=10 \ | \ 4=14=(1110)_2$ , so its niceness is equal to $4 + 3 = 7$ .
Finally, let's calculate the niceness of vertex $5$ on this path. We have $g(7,5)=a_7 \ | \ a_4 \ | \ a_2 \ | \ a_1 \ | \ a_5=10 \ | \ 4 \ | \ 7 \ | \ 4 \ | \ 10=15=(1111)_2$ and $g(5,5)=a_5=10=(1010)_2$ , so its niceness is equal to $4 + 2 = 6$ .
The maximum niceness on this path is at vertex $2$ , and it is $8$ .
 Tree from the second example, first test caseIn the first query, we have $x=7$ , $y=5$ . The shortest path from $7$ to $5$ is $7-4-2-1-5$ .
Let's calculate the niceness of vertex $7$ on this path. We have $g(7,7)=a_7=10=(1010)_2$ and $g(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 = 6$ .
Now let's calculate the niceness of vertex $4$ on this path. We have $g(7,4)=a_7 \ | \ a_4=10 \ | \ 4=14=(1110)_2$ and $g(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 = 7$ .
Now let's calculate the niceness of vertex $2$ on this path. We have $g(7,2)=a_7 \ | \ a_4 \ | \ a_2=10 \ | \ 4 \ | \ 7=15=(1111)_2$ and $g(5,2)=a_5 \ | \ a_1 \ | \ a_2=10 \ | \ 4 \ | \ 7=15=(1111)_2$ , so its niceness is equal to $4 + 4 = 8$ .
Now let's calculate the niceness of vertex $1$ on this path. We have $g(7,1)=a_7 \ | \ a_4 \ | \ a_2 \ | \ a_1=10 \ | \ 4 \ | \ 7 \ | \ 4=15=(1111)_2$ and $g(5,1)=a_5 \ | \ a_1=10 \ | \ 4=14=(1110)_2$ , so its niceness is equal to $4 + 3 = 7$ .
Finally, let's calculate the niceness of vertex $5$ on this path. We have $g(7,5)=a_7 \ | \ a_4 \ | \ a_2 \ | \ a_1 \ | \ a_5=10 \ | \ 4 \ | \ 7 \ | \ 4 \ | \ 10=15=(1111)_2$ and $g(5,5)=a_5=10=(1010)_2$ , so its niceness is equal to $4 + 2 = 6$ .
The maximum niceness on this path is at vertex $2$ , and it is $8$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted