Pitch-Black Room
Time limit2sMemory limit512 MB
Find the shortest fixed sequence of edge choices that leads every starting room to a bright room, regardless of where A starts.
- Level
Hard8 of 10
- Topics
- Graph, Bit manipulation, BFS, Dynamic programming
- Solved
- No attempts yet
Problem
A wakes up inside a pitch-black room. He seems to have wandered into a dungeon made of rooms. You could not find out which room A is in, but luckily you got hold of the dungeon map. Point out the path A should take and lead him to a bright room.
Of the rooms, are pitch-black, and you know that rooms , , , are the pitch-black ones. Also, every room has exactly one-way paths laid out in order, and the paths leaving room lead to rooms , , ..., . You can make A take the -th, -th, ..., -th paths in order from the room he is in. Once A reaches a bright room, he ignores all further instructions. You cannot know which room A is in, neither before nor after giving instructions, so the sequence of instructions you pass on must lead A to a bright room no matter which room he started in.
Among such instruction sequences, find the length of the shortest one.
Input
The input is given from standard input in the following format.
Output
Print the answer on one line.
Constraints
- All are distinct
- Every dark room can reach at least one bright room