Problem of offices
Contest: Tinkoff Challenge - Elimination Round
Difficulty rating: 2900
Tags: constructive algorithms, dfs and similar, dp, trees

Earlier, when there was no Internet, each bank had a lot of offices all around Bankopolis, and it caused a lot of problems. Namely, each day the bank had to collect cash from all the offices.

Once Oleg the bank client heard a dialogue of two cash collectors. Each day they traveled through all the departments and offices of the bank following the same route every day. The collectors started from the central department and moved between some departments or between some department and some office using special roads. Finally, they returned to the central department. The total number of departments and offices was n, the total number of roads was n - 1. In other words, the special roads system was a rooted tree in which the root was the central department, the leaves were offices, the internal vertices were departments. The collectors always followed the same route in which the number of roads was minimum possible, that is 2n - 2.

One of the collectors said that the number of offices they visited between their visits to offices a and then b (in the given order) is equal to the number of offices they visited between their visits to offices b and then a (in this order). The other collector said that the number of offices they visited between their visits to offices c and then d (in this order) is equal to the number of offices they visited between their visits to offices d and then c (in this order). The interesting part in this talk was that the shortest path (using special roads only) between any pair of offices among a, b, c and d passed through the central department.

Given the special roads map and the indexes of offices a, b, c and d, determine if the situation described by the collectors was possible, or not.

Input format:
The first line contains single integer n (5 ≤ n ≤ 5000) — the total number of offices and departments. The departments and offices are numbered from 1 to n, the central office has index 1.

The second line contains four integers a, b, c and d (2 ≤ a, b, c, d ≤ n) — the indexes of the departments mentioned in collector's dialogue. It is guaranteed that these indexes are offices (i.e. leaves of the tree), not departments. It is guaranteed that the shortest path between any pair of these offices passes through the central department.

On the third line n - 1 integers follow: p2, p3, ..., pn (1 ≤ pi < i), where pi denotes that there is a special road between the i-th office or department and the pi-th department.

Please note the joint enumeration of departments and offices.

It is guaranteed that the given graph is a tree. The offices are the leaves, the departments are the internal vertices.

Output format:
If the situation described by the cash collectors was possible, print "YES". Otherwise, print "NO".

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Note:
In the first example the following collector's route was possible: $$\left[1,3,1,5,1,2,1,4,1,3,1,5,1,2,1,4,\ldots\right]$$. We can note that between their visits to offices a and b the collectors visited the same number of offices as between visits to offices b and a; the same holds for c and d (the collectors' route is infinite as they follow it each day).

In the second example there is no route such that between their visits to offices c and d the collectors visited the same number of offices as between visits to offices d and c. Thus, there situation is impossible.

In the third example one of the following routes is: $$\left[1,2,9,2,8,2,10,2,1,4,13,4,1,5,7,5,1,3,11,3,12,3,1,2,\ldots\right]$$.

Example input:
5
2 3 4 5
1 1 1 1

Example output:
YES

Example input:
10
3 8 9 10
1 2 2 2 2 2 1 1 1

Example output:
NO

Editorial:
Formal statement: we have a rooted tree, let it contain m leaves. We can reorder sons of any vertex. We have to reorder tree in such way that while doing Eulerian tour (from vertex we go to it's first son, then to it's second son etc.) between visiting a and b we will visit exactly $$\frac{m}{2}-1$$ leaves, and same for c and d.
Let's separately consider case when tree has odd number of leaves — then answer is «NO».
Otherwise let's start visiting given leaves from a. Then we will visit c, then b and then d.
Amount of visited leaves between visiting a and b depends on how we will order subtrees on path from root to a, same for b, and also which sons of root we will place between a and b (subtree with c will always be there).
$$\text{Let a size of subtree be amount of leaves in it.}$$
After this we can notice that the set of possible distances between a and b is the set of knapsack solutions on following items: sizes of root sons except those that contain a, b, c, d, sizes of branching subtrees on path to a and sizes of branching subtrees on path to b. With these items we should get knapsack of weight equal to $$\frac{m}{2}-1-\text{size of root son, containing c.}$$
Same holds for c and d, but now items are sizes of root sons except those that contain a, b, c, d, sizes of branching subtrees on path to c and sizes of branching subtrees on path to d. With these items we should get knapsack of weight equal to $$\frac{m}{2}-1-\text{size of root son, containing b}.$$
If solution of both knapsacks exists, then answer is «YES», otherwise «NO». We can always order items from both knapsacks in needed way. Exactly, all subtrees that are present only in solution of a-b knapsack we place on path between a and c, subtrees that are present in solutions of both knapsacks we place between c and b, subtrees that are present only in solution of c-d knapsack we place between b and d, and subtrees that aren't present in any solutions we place between d and a.