标签:ini ems 题目 pre aced tin sid one ann
时间限制: 2 Sec 内存限制: 256 MB
There is a tree with N vertices, numbered 1 through N. The i-th of the N?1 edges connects vertices ai and bi.
Currently, there are Ai stones placed on vertex i. Determine whether it is possible to remove all the stones from the vertices by repeatedly performing the following operation:
Select a pair of different leaves. Then, remove exactly one stone from every vertex on the path between those two vertices. Here, a leaf is a vertex of the tree whose degree is 1, and the selected leaves themselves are also considered as vertices on the path connecting them.
Note that the operation cannot be performed if there is a vertex with no stone on the path.
Constraints
2≤N≤105
1≤ai,bi≤N
0≤Ai≤109
The given graph is a tree.
The input is given from Standard Input in the following format:
N
A1 A2 … AN
a1 b1
:
aN?1 bN?1
If it is possible to remove all the stones from the vertices, print YES. Otherwise, print NO.
5
1 2 1 1 2
2 4
5 2
3 2
1 3
样例输出
YES
提示
All the stones can be removed, as follows:
Select vertices 4 and 5. Then, there is one stone remaining on each vertex except 4.
Select vertices 1 and 5. Then, there is no stone on any vertex.
标签:ini ems 题目 pre aced tin sid one ann
原文地址:https://www.cnblogs.com/sciorz/p/8900563.html