#abc287c. C - Path Graph?

C - Path Graph?

Score : 300300 points

问题陈述

你得到一个含有 NN 个顶点和 MM 条边的简单无向图。顶点编号为 1,2,,N1, 2, \dots, N,边编号为 1,2,,M1, 2, \dots, M。 其中第 ii 条边 (i=1,2,,M)(i = 1, 2, \dots, M) 连接顶点 uiu_iviv_i

确定该图是否是一条路径图。

简单无向图是什么?简单无向图是指不含自环且不存在多条边的图,其边没有方向。

路径图是什么?如果存在一个序列 (v1,v2,,vN)(v_1, v_2, \dots, v_N),它是 (1,2,,N)(1, 2, \dots, N) 的一个排列,并满足以下条件,则具有 NN 个顶点(编号为 1,2,,N1, 2, \dots, N)的图被称作路径图

  • 对于所有 i=1,2,,N1i = 1, 2, \dots, N-1,存在一条连接顶点 viv_ivi+1v_{i+1} 的边。
  • 若整数 iijj 满足 1i,jN1 \leq i, j \leq N 以及 ij2|i - j| \geq 2,则不存在连接顶点 viv_ivjv_j 的边。

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

Problem Statement

You are given a simple undirected graph with NN vertices and MM edges. The vertices are numbered 1,2,,N1, 2, \dots, N, and the edges are numbered 1,2,,M1, 2, \dots, M.
Edge i(i=1,2,,M)i \, (i = 1, 2, \dots, M) connects vertices uiu_i and viv_i.

Determine if this graph is a path graph.

What is a simple undirected graph? A simple undirected graph is a graph without self-loops or multiple edges whose edges do not have a direction.

What is a path graph? A graph with NN vertices numbered 1,2,,N1, 2, \dots, N is said to be a path graph if and only if there is a sequence (v1,v2,,vN)(v_1, v_2, \dots, v_N) that is a permutation of (1,2,,N)(1, 2, \dots, N) and satisfies the following conditions:

  • For all i=1,2,,N1i = 1, 2, \dots, N-1, there is an edge connecting vertices viv_i and vi+1v_{i+1}.
  • If integers ii and jj satisfies 1i,jN1 \leq i, j \leq N and ij2|i - j| \geq 2, then there is no edge that connects vertices viv_i and vjv_j.

Constraints

  • 2N2×1052 \leq N \leq 2 \times 10^5
  • 0M2×1050 \leq M \leq 2 \times 10^5
  • 1ui,viN(i=1,2,,M)1 \leq u_i, v_i \leq N \, (i = 1, 2, \dots, M)
  • All values in the input are integers.
  • The graph given in the input is simple.

Input

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

NN MM

u1u_1 v1v_1

u2u_2 v2v_2

\vdots

uMu_M vMv_M

Output

Print Yes if the given graph is a path graph; print No otherwise.

Sample Input 1

4 3
1 3
4 2
3 2

Sample Output 1

Yes

Illustrated below is the given graph, which is a path graph.

example_00

Sample Input 2

2 0

Sample Output 2

No

Illustrated below is the given graph, which is not a path graph.

example_01

Sample Input 3

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

Sample Output 3

No

Illustrated below is the given graph, which is not a path graph.

example_02

update @ 2024/3/10 11:59:46