#abc318g. G - Typical Path Problem

G - Typical Path Problem

Score : 575575 points

问题描述

你被给定一个简单的连通无向图 GG,其中包含 NN 个顶点和 MM 条边。 图 GG 中的顶点和边分别标记为顶点 11、顶点 22\ldots、顶点 NN,以及边 11、边 22\ldots、边 MM,其中第 ii 条边 (1iM)(1\leq i\leq M) 连接顶点 UiU_i 和顶点 ViV_i

另外,你还被赋予了图 GG 上互不相同的三个顶点 AABBCC。你需要确定是否存在一条通过顶点 BB 连接顶点 AA 和顶点 CC 的简单路径。

什么是简单连通无向图?若一个图 GG 是无向且既是简单图又是连通图,则称其为简单连通无向图。
若图 GG 的边没有方向,则称其为无向图。
若图 GG 不包含自环和平行边,则称其为简单图。
若在图 GG 中可以通过边从所有顶点的一个到达另一个,则称其为连通图。什么是通过顶点 ZZ 的简单路径?对于图 GG 上的顶点 XXYY,连接 XXYY 的简单路径是一个由不同顶点组成的序列 (v1,v2,,vk)(v_1,v_2,\ldots,v_k),满足 v1=Xv_1=Xvk=Yv_k=Y,并且对于每个整数 ii 满足 1ik11\leq i\leq k-1,在图 GG 上存在一条连接顶点 viv_ivi+1v_{i+1} 的边。
如果对于某个 ii (2ik1)(2\leq i\leq k-1) 满足 vi=Zv_i=Z,则称该简单路径 (v1,v2,,vk)(v_1,v_2,\ldots,v_k) 是通过顶点 ZZ 的路径。

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

You are given a simple connected undirected graph GG with NN vertices and MM edges.
The vertices and edges of GG are numbered as vertex 11, vertex 22, \ldots, vertex NN, and edge 11, edge 22, \ldots, edge MM, respectively, and edge ii (1iM)(1\leq i\leq M) connects vertices UiU_i and ViV_i.

You are also given distinct vertices A,B,CA,B,C on GG. Determine if there is a simple path connecting vertices AA and CC via vertex BB.

What is a simple connected undirected graph? A graph GG is said to be a simple connected undirected graph when GG is an undirected graph that is simple and connected.
A graph GG is said to be an undirected graph when the edges of GG have no direction.
A graph GG is simple when GG does not contain self-loops or multi-edges.
A graph GG is connected when one can travel between all vertices of GG via edges. What is a simple path via vertex ZZ? For vertices XX and YY on a graph GG, a simple path connecting XX and YY is a sequence of distinct vertices (v1,v2,,vk)(v_1,v_2,\ldots,v_k) such that v1=Xv_1=X, vk=Yv_k=Y, and for every integer ii satisfying 1ik11\leq i\leq k-1, there is an edge on GG connecting vertices viv_i and vi+1v_{i+1}.
A simple path (v1,v2,,vk)(v_1,v_2,\ldots,v_k) is said to be via vertex ZZ when there is an ii (2ik1)(2\leq i\leq k-1) satisfying vi=Zv_i=Z.

Constraints

  • 3N2×1053 \leq N \leq 2\times 10^5
  • $N-1\leq M\leq\min\left(\frac{N(N-1)}{2},2\times 10^5\right)$
  • 1A,B,CN1\leq A,B,C\leq N
  • AA, BB, and CC are all distinct.
  • 1Ui<ViN1\leq U_i<V_i\leq N
  • The pairs (Ui,Vi)(U_i,V_i) are all distinct.
  • All input values are integers.

Input

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

NN MM

AA BB CC

U1U_1 V1V_1

U2U_2 V2V_2

\vdots

UMU_M VMV_M

Output

If there is a simple path that satisfies the condition in the statement, print Yes; otherwise, print No.

Sample Input 1

6 7
1 3 2
1 2
1 5
2 3
2 5
2 6
3 4
4 5

Sample Output 1

Yes

One simple path connecting vertices 11 and 22 via vertex 33 is 11 \to 55 \to 44 \to 33 \to 22.

Thus, print Yes.

Sample Input 2

6 6
1 3 2
1 2
2 3
2 5
2 6
3 4
4 5

Sample Output 2

No

No simple path satisfies the condition. Thus, print No.

Sample Input 3

3 2
1 3 2
1 2
2 3

Sample Output 3

No

update @ 2024/3/10 09:05:42