No.100 Series Amidakuji
Problem Statement
Consider an amidakuji (ladder lottery) consisting of $N$ vertical lines and horizontal lines that horizontally connect adjacent vertical lines.
We call a figure obtained by connecting $2$ amidakuji of exactly the same shape one above the other a "series amidakuji".
The figure below is an example of a series amidakuji.
Given the correspondence between the starting points and ending points of an amidakuji, determine whether there exists a "series amidakuji" that satisfies this correspondence.
Input
$N$ $a_1$ $\ldots$ $a_N$
Line $1$ contains $N(2\leq N\leq50)$, the number of vertical lines.
Line $2$ contains $a_i(1\leq a_i\leq N)$, separated by spaces, which represent the correspondence between the starting points and ending points.
This means that starting from the $i$-th vertical line from the left, you arrive at the $a_i$-th vertical line from the left.
$a_1,\ldots,a_n$ are all distinct.
Output
Print Yes if there exists a "series amidakuji" that satisfies the correspondence between the starting points and ending points, and No otherwise.
Print a newline at the end.
Sample
Sample 1
Input
2 1 2
Output
Yes
One like the figure below exists.
Sample 2
Input
3 2 3 1
Output
Yes
One like the figure below exists.
Sample 3
Input
2 2 1
Output
No
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。
sugim48