Timothy the architect has designed a new escape game. In this game, there are rooms numbered from to . Initially, each room contains exactly one key. Each key has a type, which is an integer between and , inclusive. The type of the key in room is . Note that multiple rooms may contain keys of the same type, i.e., the values are not necessarily distinct.
There are also bidirectional connectors in the game, numbered from to . Connector connects a pair of different rooms and . A pair of rooms can be connected by multiple connectors.
The game is played by a single player who collects the keys and moves between the rooms by traversing the connectors. We say that the player traverses connector when they use this connector to move from room to room , or vice versa. The player can only traverse connector if they have collected a key of type before.
At any point during the game, the player is in some room and can perform two types of actions:
- collect the key in room , whose type is (unless they have collected it already),
- traverse a connector , where either or , if the player has collected a key of type beforehand. Note that the player never discards a key they have collected.
The player starts the game in some room not carrying any keys. A room is reachable from a room , if the player who starts the game in room can perform some sequence of actions described above, and reach room .
For each room , denote the number of rooms reachable from room as . Timothy would like to know the set of indices that attain the minimum value of across .
Implementation Details
You are to implement the following procedure:
std::vector<int> find_reachable(std::vector<int> r, std::vector<int> u, std::vector<int> v, std::vector<int> c)
- : an array of length . For each , the key in room is of type .
- , : two arrays of length . For each , connector connects rooms and .
- : an array of length . For each , the type of key needed to traverse connector is .
- This procedure should return an array of length . For each , the value of should be if for every such that , . Otherwise, the value of should be .
Examples
Example 1
Consider the following call:
find_reachable({0, 1, 1, 2},
{0, 0, 1, 1, 3}, {1, 2, 2, 3, 1}, {0, 0, 1, 0, 2})
If the player starts the game in room , they can perform the following sequence of actions:
Current room | Action |
---|---|
Collect key of type | |
Traverse connector to room | |
Collect key of type | |
Traverse connector to room | |
Traverse connector to room | |
Traverse connector to room |
Hence room is reachable from room . Similarly, we can construct sequences showing that all rooms are reachable from room , which implies . The table below shows the reachable rooms for all starting rooms:
Starting room | Reachable rooms | |
---|---|---|
The smallest value of across all rooms is , and this is attained for or . Therefore, this procedure should return .
Example 2
find_reachable({0, 1, 1, 2, 2, 1, 2},
{0, 0, 1, 1, 2, 3, 3, 4, 4, 5},
{1, 2, 2, 3, 3, 4, 5, 5, 6, 6},
{0, 0, 1, 0, 0, 1, 2, 0, 2, 1})
The table below shows the reachable rooms:
Starting room | Reachable rooms | |
---|---|---|
The smallest value of across all rooms is , and this is attained for . Therefore, this procedure should return .
Example 3
find_reachable({0, 0, 0}, {0}, {1}, {0})
The table below shows the reachable rooms:
Starting room | Reachable rooms | |
---|---|---|
The smallest value of across all rooms is , and this is attained when . Therefore, this procedure should return .
Constraints
- for all
- and for all
- for all
Subtasks
- ( points) for all and
- ( points)
- ( points)
- ( points) (for all ) and (for all )
- ( points) No additional constraints.
Sample Grader
The sample grader reads the input in the following format:
- line :
- line :
- line :
The sample grader prints the return value of find_reachable
in the following format:
- line :
Attachment Package
The sample grader and sample test cases are available here: keys.zip.
Comments