码迷,mamicode.com
首页 > 其他好文 > 详细

cleaning

时间:2018-04-21 13:34:22      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:ini   ems   题目   pre   aced   tin   sid   one   ann   

Cleaning

时间限制: 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.

cleaning

标签:ini   ems   题目   pre   aced   tin   sid   one   ann   

原文地址:https://www.cnblogs.com/sciorz/p/8900563.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!