skrivaca — COCI CONTEST_#3
Task type: batch | Time limit: 2 s | Memory limit: 512 MB | Difficulty: 21
Algorithms: shortest path, shortest paths, bfs, dynamic programming

# Task Skrivača

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<sub>v</sub>*. At the start of the game Marin chooses his starting room *v<sub>0</sub>* and Luka hides in the room *a<sub>v0</sub>*. In each step of the game, firstly Marin chooses a room *u* adjacent 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<sub>u</sub>*. Notice that Luka can choose any path to reach room *a<sub>u</sub>* 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 moment 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 ≤ *n* ≤ 2 · 10<sup>5</sup>, *n* − 1 ≤ *m* ≤ min(5 · 10<sup>5</sup>, $\frac{n \cdot (n-1)}{2}$)), the number of rooms and the number of pairs of connected rooms.

In the second line there are *n* integers *a<sub>i</sub>* (1 ≤ *a<sub>i</sub>* ≤ *n*), describing Luka's hiding strategy.

In *i*-th of the next *m* lines there are integers *x<sub>i</sub>*, *y<sub>i</sub>* (1 ≤ *x<sub>i</sub>*, *y<sub>i</sub>* ≤ *n*, *x<sub>i</sub>* ≠ *y<sub>i</sub>*), denoting that room *x<sub>i</sub>* and room *y<sub>i</sub>* 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.

## Scoring

| Subtask | Points | Constraints                                                                                                                                                                                                                                                                               |
| :-------: | :----: | :---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|     1     |   15   | *n* ≤ 1000, *m* ≤ 2000                                                                                                                                                                                                                                                                 |
|     2     |   25   | *m* = *n* - 1                                                                                                                                                                                                                                                                           |
|     3     |   30   | Luka's hiding strategy will be such that he will never attempt to hide in the same or adjacent room to where Marin is currently located, and the structure of the house will be such that the game can end in at most 5 different rooms independent of Luka's hiding strategy |
|     4     |   40   | No additional constraints.                                                                                                                                                                                                                                                              |

![](_page_0_Picture_5.jpeg)

## Examples

| Input                                      | Input                                                                        | Input                                                                        |
| :----------------------------------------- | :--------------------------------------------------------------------------- | :--------------------------------------------------------------------------- |
| 4 4                                        | 8 9                                                                        | 9 8                                                                        |
| 3 4 1 2                                    | 2 3 2 1 6 5 6 7                                                            | 1 9 1 1 1 9 9 9 1                                                            |
| 1 2                                        | 1 2                                                                        | 1 2                                                                        |
| 2 3                                        | 1 3                                                                        | 2 3                                                                        |
| 3 4                                        | 2 4                                                                        | 3 4                                                                        |
| 4 1                                        | 3 4                                                                        | 4 5                                                                        |
|                                            | 4 5                                                                        | 5 6                                                                        |
|                                            | 4 6                                                                        | 6 7                                                                        |
|                                            | 6 7                                                                        | 7 8                                                                        |
|                                            | 5 7                                                                        | 8 9                                                                        |
|                                            | 4 8                                                                        |                                                                              |
| Output                                     | Output                                                                       | Output                                                                       |
| -1 -1 -1 -1                                | 1 2 2 2 1 1 1 1                                                            | 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 second step he goes back to room 4. Luka needs to pass through room 7 to get from room 1, so Marin can find Luka in 2 steps.

Starter code: