#B4045. Mutual Table Partners
Mutual Table Partners
Mutual Table Partners
There are \(2n\) children in a class, numbered \(1, 2, \ldots, 2n\). The classroom has \(n\) tables and each table seats exactly two children. If two children sit at the same table, they are table partners. A child cannot be paired with him/herself.
Each child \(i\) expresses his/her desire to sit with child \(p_i\). Help the teacher determine if it is possible to assign the seats so that every child gets to sit with his/her desired partner.
Note: In order for every child to be satisfied, the pairing must be mutual, that is, for every \(i\) the condition \(p_{p_i} = i\) must hold.
inputFormat
The first line contains a single integer \(n\) representing the number of tables. The second line contains \(2n\) integers \(p_1, p_2, \ldots, p_{2n}\), where \(p_i\) indicates the child that child \(i\) wishes to sit with.
It is guaranteed that \(p_i \neq i\) for all \(i
.outputFormat
Output a single line containing YES
if it is possible to seat the children so that every child sits with his/her desired partner, and NO
otherwise.
sample
2
2 1 4 3
YES