Keys
Time limit2sMemory limit2048 MB
Given rooms holding keys and connectors that each need a specific key type, find the starting rooms that reach the fewest rooms.
- Level
Medium7 of 10
- Topics
- Union-find, Graph, Hash map
- Solved
- No attempts yet
Problem
Timothy the architect has designed a new escape 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 . Several rooms may hold keys of the same type, so the values are not necessarily distinct.
There are bidirectional connectors numbered from to . Connector () joins two different rooms and . A pair of rooms can be joined by more than one connector.
A single player collects keys and moves between rooms through the connectors. The player traverses connector when they move from to or from to . The player can traverse connector only after collecting a key of type . The player never discards a key.
The player is in some room and can perform two kinds of actions:
- Collect the key in room , whose type is , unless it has already been collected.
- Traverse connector , where or , if the player has already collected a key of type .
The player starts in room carrying no keys. Room is reachable from room if the player starting in room can perform some sequence of these actions and end up in room .
For each room (), let be the number of rooms reachable from room . Timothy wants to know the set of indices that attain the minimum value of over .
Constraints
- for all
- and for all
- for all