Orig Description
Score : 700 points
Problem StatementThere is a tree with N vertices, numbered 1 through N.
The i-th of the N-1 edges connects vertices a_i and b_i.
Currently, there are A_i 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 ≦ 10^5
1 ≦ a_i,b_i ≦ N
0 ≦ A_i ≦ 10^9
The given graph is a tree.
InputThe input is given from Standard Input in the following format:
N
A_1 A_2 … A_N
a_1 b_1
:
a_{N-1} b_{N-1}
OutputIf it is possible to remove all the stones from the vertices, print YES. Otherwise, print NO.
Sample Input 15
1 2 1 1 2
2 4
5 2
3 2
1 3
Sample Output 1YES
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.
Sample Input 23
1 2 1
1 2
2 3
Sample Output 2NO
Sample Input 36
3 2 2 2 2 2
1 2
2 3
1 4
1 5
4 6
Sample Output 3YES
Extracted Specification
An integer N (2 ≤ N ≤ 10^5), representing some quantity or size.
A list of integers A of size N, where each integer is between 0 and 10^9.
A list of tuples of integers (a_i, b_i) of size N-1, where 1 ≤ a_i, b_i ≤ N.
### Example Input:
```
5
1 2 1 1 2
2 4
5 2
3 2
1 3
```
### Function Signature:
Write a function f(N, A, edges) that takes in the input.
```python
def f(N: int, A: List[int], edges: List[Tuple[int, int]]):
'''
N: an integer
A: a list of integers
edges: a list of tuples of integers
'''
```
Test Case Generator
To write a test case generator for the given problem statement, we need to ensure the constraints are adhered to and that the generated test cases are valid trees. Below is the Python code to generate random test cases for this problem:
```python
import random
def generate_tree_edges(N):
# Generate a random tree using the Prüfer sequence method
prufer = [random.randint(1, N) for _ in range(N - 2)]
degree = [1] * (N + 1)
for node in prufer:
degree[node] += 1
edges = []
ptr = 1
while ptr <= N and degree[ptr] != 1:
ptr += 1
leaf = ptr
for node in prufer:
edges.append((leaf, node))
degree[leaf] -= 1
if degree[node] == 1:
leaf = node
else:
ptr += 1
while ptr <= N and degree[ptr] != 1:
ptr += 1
leaf = ptr
for node in range(1, N):
if degree[node] == 1:
edges.append((leaf, node))
degree[leaf] -= 1
degree[node] -= 1
break
return edges
def tcgen():
N = random.randint(2, 10**5)
A = [random.randint(0, 10**9) for _ in range(N)]
edges = generate_tree_edges(N)
return N, A, edges
def print_test_case():
N, A, edges = tcgen()
print(N)
print(' '.join(map(str, A)))
for edge in edges:
print(f"{edge[0]} {edge[1]}")
# Example of generating and printing a test case
print_test_case()
```
### Explanation:
1. **Tree Generation**:
- The `generate_tree_edges` function generates a random tree using the Prüfer sequence method, which is a common algorithm for generating trees in competitive programming. It ensures that the generated edges form a valid tree structure.
2. **Random Values Generation**:
- `N` is randomly chosen between 2 and \(10^5\).
- The list `A` is generated with `N` random integers between 0 and \(10^9\).
3. **Output**:
- The `tcgen` function returns the tree size `N`, the list `A`, and the list of edges.
- The `print_test_case` function prints the generated test case in the required format.
This generator ensures that every test case adheres to the constraints and conditions specified in the problem statement.
Extract Arguments
def extract_arguments(fh):
N = int(fh.readline().strip())
A = list(map(int, fh.readline().strip().split()))
edges = []
for _ in range(N - 1):
a, b = map(int, fh.readline().strip().split())
edges.append((a, b))
return N, A, edges
# if __name__ == "__main__":
# import sys
# input_path = sys.argv[1]
# with open(input_path, 'r') as fh:
# N, A, edges = extract_arguments(fh)
# f(N, A, edges)