#abc277e. E - Crystal Switches
E - Crystal Switches
Score : points
问题描述
你被给予一个由 个顶点和 条边构成的无向图。
对于 ,第 条边是一个无向边,连接顶点 和 ,如果 则初始时该边是可通行的,如果 则初始时不可通行。另外,在 个顶点上存在开关:顶点 、顶点 、、顶点 。
高桥初始时位于顶点 ,他将重复执行以下两种动作之一——移动或击打开关,每次可以选择任一动作,并可以重复任意次数。
- 移动:选择当前所在顶点通过一条边相邻的一个顶点,并移动到那个顶点。
- 击打开关:如果当前所在顶点上有开关,则击打它。这将使图中每条边的通行性翻转。也就是说,原本可通行的边会变为不可通行,反之亦然。
确定高桥是否能够到达顶点 ,如果可以,请输出他在到达顶点 前至少需要执行 移动 动作的次数。
以上为通义千问 qwen-max 翻译,仅供参考。
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 .
Sample Input 1
5 5 2
1 3 0
2 3 1
5 4 1
2 1 1
1 4 0
3 4
Sample Output 1
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.
Sample Input 2
4 4 2
4 3 0
1 2 1
1 2 0
2 1 1
2 4
Sample Output 2
-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 .
update @ 2024/3/10 11:40:01