atcoder#ABC289E. [ABC289E] Swap Places
[ABC289E] Swap Places
Score : points
Problem Statement
There is a simple undirected graph with vertices numbered through and edges numbered through . Edge connects vertex and vertex . Every vertex is painted either red or blue. The color of vertex is represented by ; vertex is painted red if is and blue if is .
Now, Takahashi is on vertex and Aoki is on vertex . They may repeat the following move zero or more times.
- Each of the two simultaneously moves to a vertex adjacent to the current vertex. Here, the vertices that Takahashi and Aoki move to must have different colors.
By repeating the move above, can Takahashi and Aoki simultaneously end up on vertices and , respectively?
If it is possible, find the minimum number of moves required. If it is impossible, print -1
.
You are given at the beginning of the input. Solve the problem for test cases.
Constraints
- The graph given in the input is simple.
- All values in the input are integers.
- The sum of over all test cases does not exceed .
- The sum of over all test cases does not exceed .
Input
The input is given from Standard Input in the following format, where denotes the -th test case:
Each test case is given in the following format:
Output
Print lines. The -th line should contain the answer to the -th test case.
For each test case, print the minimum number of moves required for Takahashi and Aoki to simultaneously end up in vertices and , respectively, if it is possible, and -1
otherwise.
3
4 4
0 1 0 1
1 2
2 3
1 3
2 4
3 3
0 1 0
1 2
2 3
1 3
6 6
0 0 1 1 0 1
1 2
2 6
3 6
4 6
4 5
2 4
3
-1
3
For the -st test case, Takahashi and Aoki can achieve the objective by making the following moves, which is the minimum number:
- Takahashi moves to vertex , and Aoki moves to vertex .
- Takahashi moves to vertex , and Aoki moves to vertex .
- Takahashi moves to vertex , and Aoki moves to vertex .
Note that in the -st move, it is disallowed that both Takahashi and Aoki move to vertex (because the colors of vertices that Takahashi and Aoki move to must be different.)
For the -nd case, no matter how they move, they cannot achieve the objective.