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 N-1 pairs of integers (a_i, b_i), 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, each containing two integers
'''
```
Test Case Generator
To generate test cases for the given problem statement involving a tree with vertices and stones, we will need to create a function that generates a random tree structure, assigns a random number of stones to each vertex, and ensures the constraints are met.
Here is a Python function that does this:
```python
import random
def generate_tree_edges(N):
""" Helper function to generate a list of edges forming a tree with N nodes """
edges = []
for i in range(2, N+1):
edges.append((i, random.randint(1, i-1)))
random.shuffle(edges)
return edges
def tcgen():
N = random.randint(2, 10**5)
# Generate random stone counts for each vertex
A = [random.randint(0, 10**9) for _ in range(N)]
# Generate a random tree with N vertices
edges = generate_tree_edges(N)
return N, A, edges
# Example usage of the test case generator
if __name__ == "__main__":
N, A, edges = tcgen()
print(N)
print(' '.join(map(str, A)))
for edge in edges:
print(f"{edge[0]} {edge[1]}")
```
### Explanation:
1. **`generate_tree_edges(N)`:**
- This helper function generates a list of edges that form a tree with `N` nodes.
- It ensures that the tree is connected by always linking node `i` to a random node from `1` to `i-1`. This guarantees that the graph is a valid tree.
2. **`tcgen()`:**
- Generates a random integer `N` within the specified range.
- Creates a list `A` of size `N`, where each element is a random integer between `0` and `10^9` representing the number of stones at each vertex.
- Uses the `generate_tree_edges(N)` function to generate the list of edges, ensuring the resulting structure is a tree.
- Returns `N`, `A`, and `edges` which represent the vertices, stone counts, and edges of the tree respectively.
3. **Example Usage:**
- When `tcgen()` is called, it prints the formatted output similar to how the input would be provided for the problem:
- The number of vertices `N`.
- The list of stone counts `A`.
- The edges of the tree.
This test case generator ensures that the generated inputs are within the constraints and form a valid tree structure, suitable for testing the problem solution.
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):
edges.append(tuple(map(int, fh.readline().strip().split())))
return N, A, edges