luogu#P12017. [NOISG 2025 Finals] Reachability

[NOISG 2025 Finals] Reachability

题目描述

Sheepland is a country with nn cities. There are n1n − 1 roads connecting pairs of cities with each other. Road jj directly connects cities u[j]u[j] and v[j]v[j]. Initially, it is possible to travel from any city to any other city using only these roads.

All n1n - 1 roads in Sheepland are planned to be renovated. Under the renovation plan, each road jj will be in one of four states:

  1. Two-way: citizens from both cities u[j]u[j] and v[j]v[j] may cross this road into the other city.
  2. One-way from city u[j]u[j] to city v[j]v[j]: only citizens from city u[j]u[j] may cross this road into city v[j]v[j].
  3. One-way from city v[j]v[j] to city u[j]u[j]: only citizens from city v[j]v[j] may cross this road into city u[j]u[j].
  4. Closed: no citizens from cities u[j]u[j] or v[j]v[j] may cross this road into the other city.

Unfortunately, the renovation plan has gone missing!

To try to recover it, you ask the mayor of each city how many cities are reachable from their city under the renovation plan. The mayor of the ii-th city replies with l[i]l[i]. However, some mayors may have provided incorrect values.

A city vv is considered reachable from a city u if there exists a sequence c1,c2,c3,,ckc_1, c_2, c_3, \ldots, c_k where c1=u,ck=vc_1 = u, c_k = v and a crossable road exists from cxc_x to cx+1c_{x+1} for all 1xk11 \leq x \leq k - 1. In particular, a city is reachable from itself.

Help Sheepland determine whether there exists a renovation plan that is consistent with the number of cities reachable from each city, as reported by all mayors!

输入格式

Your program must read from standard input.

The first line of input contains one integer nn.

The second line of input contains nn space-separated integers l[1],l[2],,l[n]l[1], l[2], \ldots, l[n].

The following n1n - 1 lines of input each contain two space-separated integers. The jj-th of these lines contains u[j]u[j] and v[j]v[j].

输出格式

Your program must print to standard output.

Output YES if a renovation plan that is consistent with the number of cities reachable from each city, as reported by all mayors, exists and NO otherwise.

9
5 2 3 5 2 3 1 1 1
1 4
4 5
2 5
3 6
5 6
6 9
7 8
4 7
YES
9
5 2 3 5 2 3 1 1 2
1 4
4 5
2 5
3 6
5 6
6 9
7 8
4 7
NO
7
3 3 1 3 2 1 2
3 4
1 2
6 2
7 3
5 6
4 2
YES

提示

Subtasks

For all test cases, the input will satisfy the following bounds:

  • 1n50001 \leq n \leq 5000
  • 1l[i]n1 \leq l[i] \leq n for all 1in1 \leq i \leq n
  • 1u[j],v[j]n1 \leq u[j], v[j] \leq n for all 1jn11 \leq j \leq n - 1
  • u[j]v[j]u[j] \neq v[j] for all 1jn11 \leq j \leq n − 1
  • Initially, it is possible to travel from any city to any other city using roads only.

Your program will be tested on input instances that satisfy the following restrictions:

Subtask Marks Additional Constraints
00 Sample test cases
11 44 n7n \leq 7
22 55 n15n \leq 15
33 1111 l[1]=l[2]==l[n]l[1] = l[2] = \cdots = l[n]
44 1010 If a plan exists, at least one such plan has no two-way roads
55 4545 n400n \leq 400
66 2525 No additional constraints

Sample Test Case 1 Explanation

This test case is valid for subtasks 2,52, 5, and 66.

Refer to the diagram below. The renovation plan is consistent with the number of cities reachable from each city, as reported by all mayors.

Sample Test Case 2 Explanation

This test case is valid for subtasks 2,4,52, 4, 5, and 66.

There does not exist a renovation plan consistent with the number of cities reachable from each city, as reported by all mayors.

Sample Test Case 3 Explanation

This test case is valid for subtasks 1,2,51, 2, 5, and 66.