Skrivača
Time limit1sMemory limit1024 MB
Marin walks a graph while Luka hides in a room fixed by Marin's current position. For each start room, find the fewest steps to catch Luka, or -1.
- Level
Medium7 of 10
- Topics
- Graph, BFS, Game theory
- Solved
- No attempts yet
Statement
Marin and Luka are playing a popular kids game called Hide and Seek (Skrivača). They are playing in their house, which has rooms, and pairs of rooms are connected by a door. Rooms are labeled from to , and for each pair of rooms there exists a path from one room to another.
Luka has chosen a hiding strategy: when Marin enters room , Luka will hide in room . At the start of the game, Marin chooses his starting room , and Luka hides in room . In each step of the game, Marin first chooses a room adjacent to his current room and enters it. After that, Luka knows Marin is in room , so by his hiding strategy he hides in room . Luka can choose any path to reach room , and in one step of the game he can pass through an arbitrary number of rooms.
Marin finds Luka when both of them are in the same room, and at that moment the game ends.
Marin knows Luka's hiding strategy. For each starting room, determine whether Marin can find Luka in a finite number of steps, and if he can, the least number of steps needed if both play optimally. Marin plays so that he finds Luka in as few steps as possible, and Luka plays so that Marin needs as many steps as possible.
Input
In the first line there are integers , (, ), the number of rooms and the number of pairs of connected rooms.
In the second line there are integers (), describing Luka's hiding strategy.
In the -th of the next lines there are integers , (, ), denoting that room and room are connected. Each pair of rooms has at most one connection.
Output
Print numbers in the first and only line. The -th number is the least number of steps Marin needs to find Luka if Marin starts in room , or if Marin cannot find Luka.
Hint
Clarification of the second example: Marin enters room 8 from room 4 in the first step, and in the second step he goes back to room 4. Luka needs to pass through room 4 to get from room 7 to room 1, so Marin can find Luka in 2 steps.