Skrivača

아직 제출이 없습니다시간 제한1초메모리 제한1024 MB

문제

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 thought of a hiding strategy: when Marin enters room vv, Luka will hide in room a_va\_v. At the start of the game Marin chooses his starting room v_0v\_0 and Luka hides in the room a_v_0a\_{v\_0}. In each step of the game, firstly Marin chooses a room uu adjecent 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 a_ua\_u. Notice that Luka can choose any path to reach room a_ua\_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 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).

입력

In the first line there are integers nn, mm (1n21051 ≤ n ≤ 2 · 10^5, n1mmin(5105,n(n1)2n - 1 ≤ m ≤ \min(5 · 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 a_ia\_i (1a_in1 ≤ a\_i ≤ n), describing Luka’s hiding strategy.

In ii-th of the next mm lines there are integers x_ix\_i, y_iy\_i (1x_i,y_in1 ≤ x\_i , y\_i ≤ n, x_iy_ix\_i \ne y\_i), denoting that room x_ix\_i and room y_iy\_i are connected. Between each pair of rooms there will be at most one connection.

출력

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

힌트

Clarification of the second example: Marin enters room 88 from room 44 in the first step, and in the secend he goes back to room 44. Luka needs to pass through room 44 to get from room 77 to room 11 so Marin can find Luka in 22 steps.