#HK4011. 「eJOI2023」Tree Search
「eJOI2023」Tree Search
Description
You are given a rooted binary tree consisting of vertices. The vertices are numbered from to , the root is the vertex number . Each of the other vertices has a single parent in the tree. The tree is binary, i.e. each vertex can be a parent of at most two other vertices.
One of the vertices is special. You are trying to guess it. You can ask the questions of the following kind: "Is the special vertex in the subtree of vertex "? A node is in the subtree of vertex if and only if the shortest path between and goes through vertex . Note that vertex is also in its own subtree.
You are allowed to ask this question at most times. After that you should report your guess.
Implementation Details
You should implement the following procedure:
int solve(int N, std::vector < int > p)
- : the number of vertices
- contains exactly elements that describe the tree: the vertex (where ) is the parent of the vertex for each
- No element in p appears more than twice
- This procedure should return the number of the special vertex
- This procedure is called exactly once
The above procedure can make calls to the following procedure:
int ask(int x)
- : the number of the vertex
- returns if the special vertex is in the subtree of and otherwise
Example
Consider the following call:
solve(5, [1, 1, 2, 4])
The tree consists of the edges (1,2), (1,3), (2,4) and (4,5).

Your program made a call
ask(4)
which returned . After that your program made a call
ask(5)
which returned .
Your program concluded that vertex is special and returned .
Constraints
Subtasks
- (20 points)
- (30 points) for each
- (15 points) for each
- (35 points) No additional constraints.
Sample Grader
The sample grader reads the input in the following format:
- line 1:
- line 2:
The sample grader outputs each question in the following format:
- line 1:
The sample grader reads each answer in the following format:
- line 1:
The sample grader outputs the guess in the following format:
- line 1: