atcoder#ABC257F. [ABC257F] Teleporter Setting
[ABC257F] Teleporter Setting
Score : points
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
- $0\leq U_i
- 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 .
3 2
0 2
1 2
-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.
5 5
1 2
1 3
3 4
4 5
0 2
3 3 3 3 2