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 1e9.
A list of N-1 tuples, each containing two integers a_i and b_i (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.
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 two integers
'''
Test Case Generator
Sure, here is a test case generator for the provided problem statement involving trees and stone removal:
```python
import random
def generate_tree(N):
edges = []
if N == 2:
return [(1, 2)]
available_nodes = list(range(2, N + 1))
random.shuffle(available_nodes)
connected_nodes = [1]
while available_nodes:
u = random.choice(connected_nodes)
v = available_nodes.pop()
edges.append((u, v))
connected_nodes.append(v)
return edges
def tcgen():
N = random.randint(2, 10**5)
A = [random.randint(0, 10**9) for _ in range(N)]
edges = generate_tree(N)
return N, A, edges
# To generate and print a test case
if __name__ == "__main__":
N, A, edges = tcgen()
print(N)
print(' '.join(map(str, A)))
for a, b in edges:
print(a, b)
```
### Explanation:
1. **generate_tree(N)**: This function generates a tree with `N` nodes. It starts by connecting node 1 to other nodes one by one, ensuring that the resulting graph is a tree. It returns a list of edges.
2. **tcgen()**: This function generates a test case. It:
- Randomly selects `N` within the given constraints (2 ≤ N ≤ 10^5).
- Generates an array `A` of size `N`, with each element being a random integer between 0 and 10^9.
- Uses the `generate_tree` function to generate the edges of the tree.
- Returns `N`, `A`, and the list of edges.
### Usage:
- To generate and print a test case, run the script. It will output the number of vertices, the array of stones, and the edges of the tree.
This test case generator ensures the constraints and properties of the problem are met. The tree generation ensures that there are `N-1` edges and that the graph is connected and acyclic, as required.
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__":
# input_path = sys.argv[1]
# with open(input_path, 'r') as fh:
# N, A, edges = extract_arguments(fh)
# f(N, A, edges)