#SRD0000115. Mắc bẫy (In a Trap)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Mắc bẫy (In a Trap)
In a Trap
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
You are given an undirected tree with vertices numbered from to , rooted at vertex . Vertex contains a nonnegative integer .
Answer queries, each specified by vertices and . Vertex is guaranteed to be an ancestor of (a vertex is its own ancestor).
Let denote the set of vertices on the unique path between and , including both endpoints. Let be the number of edges on the path between and .
For each query, output
$$\max_{x\in P(u,v)} \bigl(a_x\;\operatorname{XOR}\;\operatorname{dist}(x,v)\bigr).$$Here, denotes bitwise exclusive OR. Take the maximum of the individual XOR values, not the XOR of all values on the path. The tree and its values never change.
Input
- The first line contains integers .
- The second line contains .
- Each of the next lines contains two vertices , representing an undirected edge.
- Each of the next lines contains two vertices describing a query.
The edges form a connected tree without self-loops. In every query, is an ancestor of in the tree rooted at .
Output
For every query output the maximum value on its own line, in input order.
Subtasks
- Subtask 1 (15 points): , , ; all tree/query conditions above hold.
- Subtask 2 (20 points): , , ; all tree/query conditions above hold.
- Subtask 3 (25 points): , , ; all tree/query conditions above hold.
- Subtask 4 (40 points): , , ; ; exactly edges form a tree rooted at , and is an ancestor of in every query.
Examples
Example 1
Input
5 3
0 3 2 1 4
1 2
2 3
3 4
3 5
1 4
1 5
2 4
Output
3
4
3
Explanation
For , visiting yields XOR values , so the maximum is . For , the values are , giving . For , they are , giving .
Example 2
Input
5 4
1 2 3 4 5
1 2
2 3
3 4
4 5
1 5
2 5
1 4
3 3
Output
5
5
4
3
Explanation
The tree is a chain . For , the path consists of vertex only, so the answer is . For , the value at is , and no other path vertex produces a larger value.