#P9760. [COCI 2022/2023 #3] Skrivača
[COCI 2022/2023 #3] Skrivača
Problem Description
Marin and Luka are playing a popular children's game—hide-and-seek—in their house. The house has rooms, and there are pairs of rooms connected by a door. The rooms are numbered from to , and there is a path between every two rooms.
Luka came up with a hiding strategy: when Marin enters room , Luka will hide in room . At the start of the game, Marin chooses the room where she starts searching, and Luka hides in room . In each round of the game, Marin first chooses a room adjacent to her current room and enters it. Then Luka knows that Marin is in room , so according to his hiding strategy he will hide in room . Note that Luka may choose any route to reach room , and in one round he may pass through any number of rooms.
We say Marin finds Luka when both of them are in the same room; at that moment the game ends.
Marin has discovered Luka's hiding strategy, so she wants you to determine, for each starting room, whether she can find Luka in a finite number of rounds. If she can, compute the minimum number of rounds needed under optimal play (Marin tries to minimize the number of rounds, Luka tries to maximize it).
Input Format
The first line contains two integers , representing the number of rooms and the number of connected pairs of rooms.
The second line contains integers , representing Luka's hiding strategy.
In the next lines, each line contains two integers , indicating that these two rooms are connected. There is at most one direct connection between any two rooms.
Output Format
Output one line with numbers, representing the minimum number of rounds needed. If the game cannot end in a finite number of rounds, output .
4 4
3 4 1 2
1 2
2 3
3 4
4 1
-1 -1 -1 -1
8 9
2 3 2 1 6 5 6 7
1 2
1 3
2 4
3 4
4 5
4 6
6 7
5 7
4 8
1 2 2 2 1 1 1 1
9 8
1 9 1 1 1 9 9 9 1
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
0 1 1 2 1 1 2 1 1
Hint
Sample Explanation #2.
In the first round, Marin goes from room to room , and in the second round she goes back to room . Luka must pass through room to get from room to room . Therefore, Marin can find him in two rounds.
Constraints.
| Subtask | Points | Special Properties |
|---|---|---|
| Luka's hiding strategy satisfies that he will never hide in a room adjacent to or the same as Marin's current room, and the structure of the house satisfies that the game can end with at most different rooms, independent of Luka's hiding strategy. | ||
| No special properties. |
For of the testdata, , $n-1\le m\le \min(5\times 10^5,\dfrac{n\times (n-1)}{2})$, , .
The full score for this problem is points.
Translated by ChatGPT 5