Under construction

Still being built. Things may change

Skrivača

Time limit1sMemory limit1024 MB

Summary
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 nn rooms, and mm pairs of rooms are connected by a door. Rooms are labeled from 11 to nn, 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 vv, Luka will hide in room ava_v. At the start of the game, Marin chooses his starting room v0v_0, and Luka hides in room av0a_{v_0}. In each step of the game, Marin first chooses a room uu adjacent to his current room and enters it. After that, Luka knows Marin is in room uu, so by his hiding strategy he hides in room aua_u. Luka can choose any path to reach room aua_u, 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 nn, mm (1≤n≤2⋅1051 \le n \le 2 \cdot 10^5, n−1≤m≤min⁡(5⋅105,n(n−1)2)n - 1 \le m \le \min(5 \cdot 10^5, \frac{n(n-1)}{2})), the number of rooms and the number of pairs of connected rooms.

In the second line there are nn integers aia_i (1≤ai≤n1 \le a_i \le n), describing Luka's hiding strategy.

In the ii-th of the next mm lines there are integers xix_i, yiy_i (1≤xi,yi≤n1 \le x_i, y_i \le n, xi≠yix_i \ne y_i), denoting that room xix_i and room yiy_i are connected. Each pair of rooms has at most one connection.

Output

Print nn numbers in the first and only line. The ii-th number is the least number of steps Marin needs to find Luka if Marin starts in room ii, or −1-1 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.

Examples3

  1. Example 1

    Input
    4 4
    3 4 1 2
    1 2
    2 3
    3 4
    4 1
    
    Expected output
    -1 -1 -1 -1
    
  2. 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
    
    Expected output
    1 2 2 2 1 1 1 1
    
  3. 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
    
    Expected output
    0 1 1 2 1 1 2 1 1