#abc289e. E - Swap Places
E - Swap Places
Score : points
问题描述
存在一个简单的无向图,包含 个顶点,编号为 到 ,以及 条边,编号为 到 。第 条边连接顶点 和顶点 。
每个顶点被涂成红色或蓝色。顶点 的颜色由 表示;若 为 ,则顶点 被涂成红色;若 为 ,则顶点 被涂成蓝色。
现在,Takahashi 在顶点 上,Aoki 在顶点 上。
他们可以重复执行以下操作零次或多次:
- 两人同时移动到当前顶点的相邻顶点上。
这里,Takahashi 和 Aoki 移动到的顶点必须有不同的颜色。
通过重复上述移动,Takahashi 和 Aoki 是否能够同时到达顶点 和 呢?
如果可能,找出所需的最小移动次数。如果不可能,则输出 -1
。
在输入开始时会给出 。请针对 个测试用例解决此问题。
以上为通义千问 qwen-max 翻译,仅供参考。
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.
Sample Input 1
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
Sample Output 1
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.
update @ 2024/3/10 12:04:58