AtCoder Regular Contest 191 (Div. 2)
D
Moving Pieces on Graph
AtCoder
⌀ 2631
原题
← CE →
翻译中…

Problem Statement

翻译中…

You are given a simple connected undirected graph with $N$ vertices and $M$ edges, where the vertices are numbered $1$ to $N$ and the edges are numbered $1$ to $M$. Edge $i$ connects vertex $u_i$ and vertex $v_i$ in both directions.

翻译中…

Initially, there is a piece A on vertex $S$ and a piece B on vertex $T$. Here, $S$ and $T$ are given as input.

翻译中…

You may perform the following operation any number of times in any order:

翻译中…
  • Choose either piece A or piece B, and move it from its current vertex to an adjacent vertex via an edge. However, you cannot make a move that results in both pieces ending up on the same vertex.
翻译中…

Your goal is to reach the state in which piece A is on vertex $T$ and piece B is on vertex $S$.

翻译中…

Determine whether this is possible, and if it is, find the minimum number of operations required to achieve it.

翻译中…

Constraints

翻译中…
  • $2 \le N \le 2\times 10^5$
  • $\displaystyle N-1 \le M \le \min\left(\frac{N(N-1)}{2},\,2\times 10^5\right)$
  • $1 \le u_i < v_i \le N$
  • The given graph is simple and connected.
  • $1 \le S, T \le N$
  • $S \neq T$
  • All input values are integers.
翻译中…

Input

翻译中…

The input is given from Standard Input in the following format:

翻译中…

N M S T
u_1 v_1
u_2 v_2
\vdots
u_M v_M

翻译中…

Output

翻译中…

If it is impossible to achieve the goal, print -1.

翻译中…

If it is possible, print the minimum number of operations required.

翻译中…

样例

Input #1
4 4 3 4
2 4
1 4
3 4
2 3
Output #1
3
For example, the following sequence of operations completes the goal in three moves: 1. Move piece A to vertex $2$. - Piece A is on vertex $2$, piece B is on vertex $4$. 2. Move piece B to vertex $3$. - Piece A is on vertex $2$, piece B is on vertex $3$. 3. Move piece A to vertex $4$. - Piece A is on vertex $4$, piece B is on vertex $3$. It is impossible to complete the goal in fewer than three moves, so print $3$.
Input #2
2 1 1 2
1 2
Output #2
-1
No matter how you move the pieces, you cannot achieve the goal.
Input #3
5 6 3 5
1 2
2 3
1 5
2 4
1 3
2 5
Output #3
4
题目信息
题号
arc191_d
时间限制
2 s
内存限制
1024 MB

题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。