Brave hero Bitaro challenges a dungeon quest.
The dungeon consists of rooms numbered from . Each room except room has exactly one parent room. The parent room of room () is room , and conversely, room is a child room of room . A room can have multiple child rooms, and room does not have a parent room.
Bitaro explores the rooms according to the following rules.
- Initially, he must explore room .
- After that, among the unexplored rooms, he chooses and explores one room whose parent room has already been explored.
Room in the dungeon has a difficulty and a level . The level of each room does not change. The difficulty is defined as the number of already explored rooms among the rooms that can be reached by moving or more times from room towards its child rooms. For example, the difficulty of an unexplored room is , and the difficulty of room is always the number of rooms explored so far.
Every time a new room is explored, the following rules are applied to all rooms in the dungeon. Accordingly, monsters may be added to some rooms. Note that monsters can be added to already explored rooms.
- If , monsters are newly added to room .
- If , no monsters are added to room .
- No monsters are added to a room with a difficulty of (an unexplored room).
Since Bitaro wants to minimize fighting, when exploring a new room, he selects the room that minimizes the total number of newly added monsters in the entire dungeon immediately after the exploration. If there are multiple such rooms, he selects the room with the smallest number.
However, Bitaro knows but does not know . To find out, he can choose and investigate one of the explorable rooms to learn its level .
Help Bitaro successfully complete the dungeon quest.
Implementation Details
Before implementing the functions, you must include the "brave.h" header file using #include "brave.h".
You must implement the following function.
void bitaroTravel(int N, std::vector P)
- : the number of rooms
- : an array of length representing the parent room of each room (defined as )
- This function is called exactly once per test case.
- Inside this function, you must call the following two functions to simulate Bitaro's exploration.
int explore(int i)
- : the number of the room to newly explore ()
- In the first call, you must explore room .
- In subsequent calls, room must be unexplored, and room must already be explored.
- If the condition is not met, it returns . (This is independent of whether the exploration rule is violated.)
- Otherwise, it returns .
int investigate(int i)
- : the room number to investigate ()
- At this time, room must be unexplored, and room must be explored.
- Every room can be investigated at most once. That is, you cannot investigate the same room multiple times.
- If the conditions are not satisfied, it returns .
- Otherwise, it returns .
After exploring all rooms by calling the explore function a total of times, the bitaroTravel function must terminate.
After termination, if the order of exploring the rooms meets the problem's conditions, you will receive an Accepted!! verdict; otherwise, you will receive a Wrong Answer verdict.You must not execute any I/O functions in any part of the submitted source code.
Constraints
- .
- ().
- .
- ().