#P11354. [eJOI 2023] Tree Search
[eJOI 2023] Tree Search
Background
This problem has been adapted to Luogu’s interactive format. You can implement your solution based on the following code:
#include<bits/stdc++.h>
using namespace std;
extern "C" bool ask(int x);
extern "C" int solve(int n,vector<int> p)
{
assert(ask(1)==1);
return 20080520;
}
Problem Description
This is an interactive problem.
You are given a rooted binary tree with nodes, and the root is guaranteed to have index .
Exactly one unknown node in the tree is special, and you need to find this node.
Each time, you may ask the interactive library whether a subtree contains the special node. You may ask at most times.
Implementation Details
You need to implement the following function:
int solve(int N, std::vector < int > p)
- : the number of nodes in the tree.
- : an array of length describing the shape of the tree. The -th element satisfies and represents the parent index of node .
- All elements in are pairwise distinct.
- You need to return the index of the special node.
- This function will be called exactly once.
You may call the following function:
int ask(int x)
- : the index of the root of the subtree being queried.
- You must ensure .
- If the subtree contains the special node, it returns ; otherwise it returns .
Input Format
The following is the input format of the provided grader. You should not read any information from standard input.
The first line contains an integer .
The second line contains integers .
Then, for each query, an integer is given representing the return value of the query.
Output Format
The following is the output format of the provided grader. You should not print any information to standard output.
For each query, output one line ? x, where is the node being queried.
Finally, output one line ! x, where is the index of the guessed node.
5
1 1 2 4
1
0
? 4
? 5
! 4
Hint
Constraints
This problem uses bundled testdata.
- Subtask 1 (20 pts): .
- Subtask 2 (30 pts): .
- Subtask 3 (15 pts): .
- Subtask 4 (35 pts): no special constraints.
For of the testdata, it is guaranteed that .
Translated by ChatGPT 5