Path Graph?

AtCoder
IDabc287_c
Time2000ms
Memory256MB
Difficulty
You are given a simple undirected graph with $N$ vertices and $M$ edges. The vertices are numbered $1, 2, \dots, N$, and the edges are numbered $1, 2, \dots, M$. Edge $i \, (i = 1, 2, \dots, M)$ connects vertices $u_i$ and $v_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, \dots, N-1$, there is an edge connecting vertices $v_i$ and $v_{i+1}$. * If integers $i$ and $j$ satisfies $1 \leq i, j \leq N$ and $|i - j| \geq 2$, then there is no edge that connects vertices $v_i$ and $v_j$. ## Constraints * $2 \leq N \leq 2 \times 10^5$ * $0 \leq M \leq 2 \times 10^5$ * $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: $N$ $M$ $u_1$ $v_1$ $u_2$ $v_2$ $\vdots$ $u_M$ $v_M$ [samples]
Samples
Input #1
4 3
1 3
4 2
3 2
Output #1
Yes

Illustrated below is the given graph, which is a path graph.
![image](https://img.atcoder.jp/abc287/59d45566ae7f7fd4df9801eb0fdbea5f.png)
Input #2
2 0
Output #2
No

Illustrated below is the given graph, which is not a path graph.
![image](https://img.atcoder.jp/abc287/6c608de40ba7875deaf1aa168c7f8c83.png)
Input #3
5 5
1 2
2 3
3 4
4 5
5 1
Output #3
No

Illustrated below is the given graph, which is not a path graph.
![image](https://img.atcoder.jp/abc287/73f11a6a7687f4e373da69426883e134.png)
API Response (JSON)
{
  "problem": {
    "name": "Path Graph?",
    "description": {
      "content": "You are given a simple undirected graph with $N$ vertices and $M$ edges. The vertices are numbered $1, 2, \\dots, N$, and the edges are numbered $1, 2, \\dots, M$.   Edge $i \\, (i = 1, 2, \\dots, M)$ con",
      "description_type": "Markdown"
    },
    "platform": "AtCoder",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "abc287_c"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "You are given a simple undirected graph with $N$ vertices and $M$ edges. The vertices are numbered $1, 2, \\dots, N$, and the edges are numbered $1, 2, \\dots, M$.  \nEdge $i \\, (i = 1, 2, \\dots, M)$ con...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments