#abc257f. F - Teleporter Setting

F - Teleporter Setting

Score : 500500 points

问题陈述

NN 个编号为 Town 11、Town 22\ldots、Town NN 的城镇。

同时存在 MM传送器,每个传送器双向连接两个城镇,使得一个人可以在一分钟内从一个城镇到达另一个城镇。

ii 个传送器双向连接 Town UiU_i 和 Town ViV_i。然而,对于其中一些传送器来说,其连接的一个城镇是未确定的;若 Ui=0U_i=0 表示第 ii 个传送器连接的是 Town ViV_i,但另一端是未确定的。

对于 i=1,2,,Ni=1,2,\ldots,N,回答以下问题:

当所有具有未确定终点的传送器都被确定连接到 Town ii 时,从 Town 11 到 Town NN 所需的最短分钟数是多少?如果仅使用传送器无法从 Towns 11NN 进行旅行,则输出 1-1

以上为通义千问 qwen-max 翻译,仅供参考。

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
  • 0Ui<ViN0\leq U_i<V_i\leq N
  • 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.

Sample Input 1

3 2
0 2
1 2

Sample Output 1

-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.

Sample Input 2

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

Sample Output 2

3 3 3 3 2

update @ 2024/3/10 10:55:55