Problem list
Log in
#coci00638 · COCI 2022/2023 Contest 3

Skrivača

Unrated 2.0 s 512 MB

Marin and Luka are playing a popular kids game called Hide and Seek (Skrivača). They are playing in their house which has $n$ rooms, and $m$ pairs of rooms are connected by a door. Rooms are labeled from 1 to $n$ and for each pair of rooms there exists a path from one room to another. Luka has thought of a hiding strategy: when Marin enters room $v$, Luka will hide in room $a_{v}$. At the start of the game Marin chooses his starting room $v_{0}$ and Luka hides in the room $a_{v0}$. In each step of the game, firstly Marin chooses a room $u$ adjecent to his current room and enters it. After that, Luka knows Marin is in room $u$ so by his hiding strategy he hides in room $a_{u}$. Notice that Luka can choose any path to reach room $a_{u}$ and that in one step of the game he can pass through an arbitrary number of rooms. Marin will find Luka when both of them are located in the same room, at that momement the game ends. Marin found out about Luka’s hiding strategy so he wants you to determine for each starting room if Marin can find Luka in a finite amount of steps, and if he can, determine the least amount of steps necessary if both of them play optimally. (Marin plays such that he finds Luka in the least amount of steps and Luka plays such that Marin finds him in the most amount of steps).

Input

In the first line there are integers $n$, $m$ ($1 \le n \le 2 \cdot 10^{5}$, $n - 1 \le m \le mi$n($5 \cdot 10^{5}$, ^{n·($n - 1$)} 2 ), the number of rooms and the number of pairs of connected rooms. In the second line there are $n$ integers $a_{i}$ ($1 \le a_{i} \le n$), describing Luka’s hiding strategy. In $i$-th of the next $m$ lines there are integers $x_{i}$, $y_{i}$ ($1 \le x_{i}$, $y_{i} \le n$, $x_{i}$̸ = $y_{i}$), denoting that room $x_{i}$ and room $y_{i}$ are connected. Between each pair of rooms there will be at most one connection.

Output

In the first and only line print $n$ numbers, where the $i$-th number represents the least amount of steps necessary for Marin to find Luka if Marin starts in room $i$, $or -1$ if Marin can’t find Luka.

Example 1

Input
4 4
3 4 1 2
1 2
2 3
3 4
4 1
Output
-1 -1 -1 -1

Example 2

Input
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
Output
1 2 2 2 1 1 1 1

Example 3

Input
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
Output
0 1 1 2 1 1 2 1 1

Clarification of the second example: Marin enters room 8 from room 4 in the first step, and in the secend 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.

Source
COCI 2022/2023 Contest 3
Appears in
Not part of a ladder, contest or set yet.
Accepted 0 Submissions 0 Acceptance 0% Solvers 0

Discussion

Threads about this problem. Spoilers are collapsed; wrap hints in ||double bars||.

No discussion yet. Start a thread if something here needs explaining.

Recent submissions Open with filters
Code