#P9386. Difficulty Assignment Verification
Difficulty Assignment Verification
Difficulty Assignment Verification
In the ION syllabus there are (n) knowledge points. Some of these points have already been assigned a difficulty by small I, and the remaining have not. The dependency relations between these knowledge points form an outward tree rooted at node 1. An edge from node (x) to node (y) ((x \to y)) indicates that knowledge point (x) depends on knowledge point (y).
A determined difficulty assignment is considered valid if and only if there exists a way to assign a nonnegative integer difficulty to every undetermined knowledge point, so that for every knowledge point (x) that depends on at least one other knowledge point (i.e. has at least one outgoing edge), the following condition holds. Let (\max_x) be the maximum difficulty among all knowledge points that (x) depends on. Then if (x) depends on exactly one knowledge point with difficulty (\max_x) the difficulty of (x) must be (\max_x); otherwise, the difficulty of (x) must be (\max_x + 1). (For knowledge points which do not depend on any other, there is no restriction.)
You are given the tree structure and a partially determined difficulty assignment. For each knowledge point, a value of -1 indicates that its difficulty is not yet determined, and a nonnegative integer is a fixed difficulty. Determine whether the already-set difficulties are reasonable, i.e. whether it is possible to assign values to the undetermined nodes so that all conditions are satisfied.
inputFormat
The first line contains an integer (n) ((1 \le n \le 10^5)), the number of knowledge points.
The second line contains (n) integers (a_1, a_2, \dots, a_n) where (a_i \ge 0) indicates the predetermined difficulty for node (i) and (-1) indicates that the difficulty is not determined.
Then follow (n-1) lines. For each (i) from 2 to (n), the (i)th line contains a single integer (p_i) ((1 \le p_i \le n)) indicating that there is an edge from node (p_i) to node (i) (i.e. node (p_i) depends on node (i)). It is guaranteed that these edges form an outward tree rooted at node 1.
outputFormat
Output a single line containing either “YES” if it is possible to assign the undetermined difficulties so that the conditions are satisfied, or “NO” otherwise.
sample
3
-1 1 0
1
1
YES