atcoder#ABC257F. [ABC257F] Teleporter Setting

[ABC257F] Teleporter Setting

Score : 500500 points

Problem Statement

There are NN towns numbered Town 11, Town 22, \ldots, Town NN. There are also MM Teleporters, each of which connects two towns bidirectionally so that a person can travel from one to the other in one minute.

The ii-th Teleporter connects Town UiU_i and Town ViV_i bidirectionally. However, for some of the Teleporters, one of the towns it connects is undetermined; Ui=0U_i=0 means that one of the towns the ii-th Teleporter connects is Town ViV_i, but the other end is undetermined.

For i=1,2,,Ni=1,2,\ldots,N, answer the following question.

When the Teleporters with undetermined ends are all determined to be connected to Town ii, how many minutes is required at minimum to travel from Town 11 to Town NN? If it is impossible to travel from Towns 11 to NN using Teleporters only, print 1-1 instead.

Constraints

  • 2N3×1052 \leq N \leq 3\times 10^5
  • 1M3×1051\leq M\leq 3\times 10^5
  • $0\leq U_i
  • If iji \neq j, then (Ui,Vi)(Uj,Vj)(U_i,V_i)\neq (U_j,V_j).
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN MM

U1U_1 V1V_1

U2U_2 V2V_2

\vdots

UMU_M VMV_M

Output

Print NN integers, with spaces in between. The kk-th integer should be the answer to the question when i=ki=k.

3 2
0 2
1 2
-1 -1 2

When the Teleporters with an undetermined end are all determined to be connected to Town 11, the 11-st and the 22-nd Teleporters both connect Towns 11 and 22. Then, it is impossible to travel from Town 11 to Town 33.

When the Teleporters with an undetermined end are all determined to be connected to Town 22, the 11-st Teleporter connects Town 22 and itself, and the 22-nd one connects Towns 11 and 22. Again, it is impossible to travel from Town 11 to Town 33.

When the Teleporters with an undetermined end are all determined to be connected to Town 33, the 11-st Teleporter connects Town 33 and Town 22, and the 22-nd one connects Towns 11 and 22. In this case, we can travel from Town 11 to Town 33 in two minutes.

  • Use the 22-nd Teleporter to travel from Town 11 to Town 22.
  • Use the 11-st Teleporter to travel from Town 22 to Town 33.

Therefore, 1,1-1,-1, and 22 should be printed in this order.

Note that, depending on which town the Teleporters with an undetermined end are connected to, there may be a Teleporter that connects a town and itself, or multiple Teleporters that connect the same pair of towns.

5 5
1 2
1 3
3 4
4 5
0 2
3 3 3 3 2