atcoder#ABC277E. [ABC277E] Crystal Switches
[ABC277E] Crystal Switches
Score : points
Problem Statement
You are given an undirected graph consisting of vertices and edges. For , the -th edge is an undirected edge connecting vertex and that is initially passable if and initially impassable if . Additionally, there are switches on of the vertices: vertex , vertex , , vertex .
Takahashi is initially on vertex , and will repeat performing one of the two actions below, Move or Hit Switch, which he may choose each time, as many times as he wants.
- Move: Choose a vertex adjacent to the vertex he is currently on via an edge, and move to that vertex.
- Hit Switch: If there is a switch on the vertex he is currently on, hit it. This will invert the passability of every edge in the graph. That is, a passable edge will become impassable, and vice versa.
Determine whether Takahashi can reach vertex , and if he can, print the minimum possible number of times he performs Move before reaching vertex .
Constraints
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
Output
If Takahashi cannot reach vertex , print ; if he can, print the minimum possible number of times he performs Move before reaching vertex .
5 5 2
1 3 0
2 3 1
5 4 1
2 1 1
1 4 0
3 4
5
Takahashi can reach vertex as follows.
- Move from vertex to vertex .
- Move from vertex to vertex .
- Hit the switch on vertex . This inverts the passability of every edge in the graph.
- Move from vertex to vertex .
- Move from vertex to vertex .
- Hit the switch on vertex . This again inverts the passability of every edge in the graph.
- Move from vertex to vertex .
Here, Move is performed five times, which is the minimum possible number.
4 4 2
4 3 0
1 2 1
1 2 0
2 1 1
2 4
-1
The given graph may be disconnected or contain multi-edges. In this sample input, there is no way for Takahashi to reach vertex , so you should print .