#P12017. Renovation Plan Recovery
Renovation Plan Recovery
Renovation Plan Recovery
Sheepland is a country with \( n \) cities and \( n-1 \) roads forming a tree. Each road \( j \) connects cities \( u[j] \) and \( v[j] \). The roads are to be renovated and each road will end up in one of four states:
- Two‐way: both \( u[j] \) and \( v[j] \) can cross in either direction.
- One‐way from \( u[j] \) to \( v[j] \): only citizens from \( u[j] \) can cross towards \( v[j] \).
- One‐way from \( v[j] \) to \( u[j] \): only citizens from \( v[j] \) can cross towards \( u[j] \).
- Closed: no one can cross.
A mayor in each city \( i \) reports a number \( l[i] \) representing the total number of cities that are reachable from city \( i \) under the renovation plan. A city \( v \) is reachable from a city \( u \) if there exists a sequence of cities \( c_1, c_2, \ldots, c_k \) with \( c_1=u \) and \( c_k=v \) such that for every consecutive pair \( c_x \) to \( c_{x+1} \) there exists a road which is crossable in that direction. Note that each city is reachable from itself.
Your task is to determine whether there exists an assignment of states to the \( n-1 \) roads so that the reachable city count from every city is exactly as reported by the mayor. If such a renovation plan exists, output YES
; otherwise, output NO
.
Note: It is guaranteed that the input forms a tree and \( 1 \leq l[i] \leq n \) for every \( i \).
inputFormat
The input begins with a single integer \( n \) (\( 1 \le n \le 10 \)), the number of cities. The next line contains \( n \) integers, \( l[1], l[2], \ldots, l[n] \), where \( l[i] \) is the number of cities reachable from city \( i \). Then follow \( n-1 \) lines each containing two integers \( u \) and \( v \) (1-indexed), denoting a bidirectional road between cities \( u \) and \( v \).
It is guaranteed that the \( n-1 \) roads form a tree.
outputFormat
Output a single line containing YES
if a renovation plan exists such that for each city the number of reachable cities is exactly as reported; otherwise, output NO
.
sample
3
3 3 3
1 2
2 3
YES