#abc257f. F - Teleporter Setting
F - Teleporter Setting
Score : points
问题陈述
有 个编号为 Town 、Town 、、Town 的城镇。
同时存在 个 传送器,每个传送器双向连接两个城镇,使得一个人可以在一分钟内从一个城镇到达另一个城镇。
第 个传送器双向连接 Town 和 Town 。然而,对于其中一些传送器来说,其连接的一个城镇是未确定的;若 表示第 个传送器连接的是 Town ,但另一端是未确定的。
对于 ,回答以下问题:
当所有具有未确定终点的传送器都被确定连接到 Town 时,从 Town 到 Town 所需的最短分钟数是多少?如果仅使用传送器无法从 Towns 到 进行旅行,则输出 。
以上为通义千问 qwen-max 翻译,仅供参考。
Problem Statement
There are towns numbered Town , Town , , Town .
There are also Teleporters, each of which connects two towns bidirectionally so that a person can travel from one to the other in one minute.
The -th Teleporter connects Town and Town bidirectionally. However, for some of the Teleporters, one of the towns it connects is undetermined; means that one of the towns the -th Teleporter connects is Town , but the other end is undetermined.
For , answer the following question.
When the Teleporters with undetermined ends are all determined to be connected to Town , how many minutes is required at minimum to travel from Town to Town ? If it is impossible to travel from Towns to using Teleporters only, print instead.
Constraints
- If , then .
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
Print integers, with spaces in between. The -th integer should be the answer to the question when .
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 ,
the -st and the -nd Teleporters both connect Towns and . Then, it is impossible to travel from Town to Town .
When the Teleporters with an undetermined end are all determined to be connected to Town ,
the -st Teleporter connects Town and itself, and the -nd one connects Towns and . Again, it is impossible to travel from Town to Town .
When the Teleporters with an undetermined end are all determined to be connected to Town ,
the -st Teleporter connects Town and Town , and the -nd one connects Towns and . In this case, we can travel from Town to Town in two minutes.
- Use the -nd Teleporter to travel from Town to Town .
- Use the -st Teleporter to travel from Town to Town .
Therefore, , and 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