#4528. Path Graph?

0

Path Graph?

Path Graph?

题目描述

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 $N$ vertices numbered $1, 2, \dots, N$ is said to be a path graph if and only if there is a sequence $(v_1, v_2, \dots, v_N)$ that is a permutation of $(1, 2, \dots, N)$ and satisfies the following conditions:

For all i=1,2,…,N−1i = 1, 2, \dots, N-1, there is an edge connecting vertices viv_i and vi+1v_{i+1}. If integers ii and jj satisfies 1≤i,j≤N1 \leq i, j \leq N and ∣i−j∣≥2|i - j| \geq 2, then there is no edge that connects vertices viv_i and vjv_j.

输入格式

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

输出格式

Print YesYes if the given graph is a path graph; print NoNo otherwise.

4 3
1 3
4 2
3 2
Yes
2 0
No
5 5
1 2
2 3
3 4
4 5
5 1
No

提示

2≤N≤2×1052 \leq N \leq 2 \times 10^5 0≤M≤2×1050 \leq M \leq 2 \times 10^5 1≤ui,vi≤N (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.Illustrated below is the given graph, which is a path graph. example_00
Illustrated below is the given graph, which is not a path graph. example_01
Illustrated below is the given graph, which is not a path graph. example_02

标签: AtCoder|abc287C

来源

AtCoder|abc287C