#P16705. [SEATST 2026] XOR 传送 / XOR Teleport
[SEATST 2026] XOR 传送 / XOR Teleport
Problem Description
You are given a weighted tree with vertices, numbered from to . For each with , vertex is connected to its parent vertex () by an edge with weight (). Note that vertex has no parent; for convenience, we set .
The only way for Sasaki to move on the tree is by teleportation. Sasaki can teleport from vertex to vertex using energy if and only if all of the following conditions hold:
- is an ancestor of , or is an ancestor of , and
- the bitwise XOR sum of all edge weights on the path from to is at most .
Note: Each teleport does not consume energy; after each teleport, Sasaki still has energy.
::::info[When is an ancestor of ?]{open} Vertex is an ancestor of vertex if at least one of the following is true:
- Vertex is vertex itself (), or
- Vertex is the parent of vertex (), or
- Vertex is the parent of the parent of vertex (), or
- Vertex is the parent of the parent of the parent of vertex (), or
- and so on. ::::
::::info[What is the bitwise XOR sum (XOR)?]{open} The bitwise XOR sum of two non-negative integers and (denoted by ) is defined as follows:
- When is written in binary, for the digit at , the result is if exactly one of and has a at that digit; otherwise it is .
For example:
- (in binary: ).
- (in binary: ).
The bitwise XOR of multiple integers is defined as $A[0] \oplus A[1] \oplus A[2] \oplus ... \oplus A[K - 1]$.
Note that is commutative and associative. That is, and . Therefore, the final result does not depend on the order of the integers or the order of the XOR operations. ::::
Miyako needs to answer queries. Each query is specified by a pair of integers and . Miyako's task is to compute the minimum energy required for Sasaki to reach vertex from vertex using zero or more teleport operations.
Implementation Details
You need to implement the following functions:
void init(int N, std::vector<int> P, std::vector<int> W)
- : the number of vertices in the tree.
- : integer arrays of length that specify each vertex's parent and the connecting edge weight, respectively.
- This function is called exactly once at the beginning (before any calls to
minimum_energy).
int minimum_energy(int U, int V)
- : a pair of integers describing one query.
- This function is called exactly times after
initis called. - This function should return the answer to the given query.
Input Format
N
P[1] P[2] ... P[N - 1]
W[1] W[2] ... W[N - 1]
Q
U[0] V[0]
U[1] V[1]
...
U[Q - 1] V[Q - 1]
Here, and (for all ) are the input parameters of the -th call to minimum_energy.
Output Format
A[0]
A[1]
...
A[Q - 1]
Here, is the answer to the -th query (for all ).
Hint
Samples
Consider the following function call:
init(6, [-1, 0, 1, 0, 1, 2], [-1, 3, 2, 0, 2, 1])
This tree has vertices, as shown in the figure below.
:::align{center}
:::
minimum_energy(2, 4)
Sasaki can use the following teleports, requiring energy to move from vertex to vertex :
- Teleport from vertex to vertex . Vertex is an ancestor of vertex , and the bitwise XOR sum of edge weights on the path from vertex to vertex is .
- Teleport from vertex to vertex . Vertex is an ancestor of vertex , and the bitwise XOR sum of edge weights on the path from vertex to vertex is .
There is no teleport sequence that uses strictly less energy. Therefore, this call should return .
minimum_energy(3, 0)
Sasaki can use the following teleport, requiring energy to move from vertex to vertex :
- Teleport from vertex to vertex . Vertex is an ancestor of vertex , and the bitwise XOR sum of edge weights on the path from vertex to vertex is .
Therefore, this call should return .
minimum_energy(1, 1)
Since both the start and the destination are vertex , Sasaki does not need to teleport at all, so the required energy is zero. Therefore, this call should return .
minimum_energy(0, 5)
Sasaki can use the following teleport, requiring energy to move from vertex to vertex :
- Teleport from vertex to vertex . Vertex is an ancestor of vertex , and the bitwise XOR sum of edge weights on the path from vertex to vertex is .
Therefore, this call should return .
Constraints
- .
- 。
- .
- For all , .
- .
- For all , .
- In each query, .
Subtasks
- ( points) .
- ( points) For all , .
- ( points) .
- ( points) Same as above.
- ( points) For all , .
- ( points) .
- ( points) No additional constraints.
Translated by ChatGPT 5