P23082 [AtCoder] [ABC021B] The Lying Tale of Takahashi
入门
题目描述

Problem Description

You and Takahashi live in the AtCoder Kingdom. The AtCoder Kingdom has $ N $ towns and several roads connecting these towns, allowing bidirectional movement. The $ N $ towns are named Town $ 1 $, Town $ 2 $, ..., Town $ N $.

Takahashi decided to visit your house. He started from Town $ a $ and arrived at your house in Town $ b $ after exactly $ K $ stops at some towns in the AtCoder Kingdom.

Takahashi claims that he traveled the shortest path from Town $ a $ to Town $ b $, but you suspect he is lying. However, you do not know the exact configuration of the roads connecting the towns, so you cannot determine if the path he took was indeed the shortest.

You managed to find out the sequence of towns he visited, excluding the starting and ending towns, Town $ a $ and Town $ b $.

Based on this information, you decided to write a program that determines if it is possible for Takahashi to have traveled the shortest path. The shortest path from Town $ a $ to Town $ b $ is defined as the path with the minimum number of road crossings.

If there exists at least one configuration of towns and roads such that the path Takahashi took is the shortest path, then it is possible that he traveled the shortest path. Otherwise, it is not possible.

Input Format

The input is given from the standard input in the following format:

$ N $ $ a $ $ b $ $ K $ $ P_1 $ $ P_2 $ … $ P_K $

  • The first line contains an integer $ N(2≦N≦100) $, representing the number of towns in the AtCoder Kingdom.
  • The second line contains two integers $ a,b(1≦a,b≦N,a≠b) $, representing the town where Takahashi started and the town where your house is, separated by a space.
  • The third line contains an integer $K\ (1≦K≦100)$, representing the number of towns Takahashi visited during his journey.
  • The fourth line contains the sequence of town numbers Takahashi visited in order, separated by spaces. The $ i(1≦i≦K) $th integer represents the town number $ P_i(1≦P_i≦N) $ that Takahashi visited after leaving Town $ a $.
  • All adjacent elements in $\{P_i\}$ are distinct. That is, for all $ j(2≦j≦K) $, $ P_j≠P_{j-1} $ holds. Additionally, $ P_1 ≠ a $ and $ P_K ≠ b $ hold.

Output Format

Output a single line with YES if it is possible that Takahashi traveled the shortest path, or NO if it is not possible.

Do not forget the newline at the end.

Sample Input and Output

Sample Input #1

5
1 5
3
3 4 2

Sample Output #1

YES

Sample Input #2

7
1 3
4
2 4 2 7

Sample Output #2

NO

Sample Input #3

4
1 4
3
2 1 3

Sample Output #3

NO

Sample Input #4

4
1 4
3
2 4 3

Sample Output #4

NO

Sample Input #5

20
1 4
12
2 3 5 7 8 9 10 11 12 15 13 14

Sample Output #5

YES

Notes/Hints

Sample Explanation 1

Consider the following road configuration: $ 1→3→4→2→5 $ is a shortest path.

Sample Explanation 2

There is no road configuration that allows for the shortest path $ 1→2→4→2→7→3 $. Any movement that revisits a town can always be omitted.

Sample Explanation 3

Takahashi moved $ 1→2→1→3→4 $. He returned to the starting point, which cannot be part of the shortest path.

Sample Explanation 4

Takahashi moved $ 1→2→4→3→4 $. He reached the destination but continued moving.

输入格式

见题面 Input 部分

输出格式

见题面 Output 部分

样例输入
5 1 5 3 3 4 2
样例输出
YES