Problem p03159 - Generation 3

Orig Description

Score : 600 points
Problem StatementThe server in company A has a structure where N devices numbered 1, 2, ..., N are connected with N - 1 cables.
The i-th cable connects Device U_i and Device V_i.
Any two different devices are connected through some number of cables.
Each device v (1 \leq v \leq N) has a non-zero integer A_v, which represents the following:
If A_v < 0, Device v is a computer that consumes an electric power of -A_v.
If A_v > 0, Device v is a battery that supplies an electric power of A_v.
You have decided to disconnect some number of cables (possibly zero) to disable the server.
When some cables are disconnected, the devices will be divided into some number of connected components.
The server will be disabled if all of these connected components satisfy one of the following conditions:
There is no computer in the connected component. That is, A_v is positive for every device v that belongs to the connected component.
There is not enough supply of electric power in the connected component. That is, the sum of A_v over all devices v that belong to the connected component is negative.
At least how many cables do you need to disconnect in order to disable the server?
Constraints
1 \leq N \leq 5 000
1 \leq |A_i| \leq 10^9 (1 \leq i \leq N)
1 \leq U_i, V_i \leq N (1 \leq i \leq N - 1)
U_i \neq V_i (1 \leq i \leq N - 1)
Any two different devices are connected through some number of cables.
All values in input are integers.
InputInput is given from Standard Input in the following format:
N
A_1 A_2 ... A_N
U_1 V_1
U_2 V_2
:
U_{N - 1} V_{N - 1}
OutputPrint the answer.
Sample Input 17
-2 7 5 6 -8 3 4
1 2
2 3
2 4
1 5
5 6
5 7
Sample Output 11
We should disconnect the cable connecting Device 1 and Device 2.
Sample Input 24
1 2 3 4
1 2
1 3
1 4
Sample Output 20
Sample Input 36
10 -1 10 -1 10 -1
1 2
2 3
3 4
4 5
5 6
Sample Output 35
Sample Input 48
-2 3 6 -2 -2 -5 3 2
3 4
7 6
6 2
8 2
5 3
1 8
3 7
Sample Output 43
Sample Input 510
3 4 9 6 1 5 -1 10 -10 -10
7 4
5 6
8 1
9 5
7 1
10 3
2 8
4 10
9 2
Sample Output 53

Extracted Specification

An integer \(N\) (1 ≤ \(N\) ≤ 5000), representing some quantity or size.
A list of \(N\) integers, where each integer is between -10^9 and 10^9.
A list of \(N-1\) pairs of integers, where each integer is between 1 and \(N\).

### Example Input:

```
7
-2 7 5 6 -8 3 4
1 2
2 3
2 4
1 5
5 6
5 7
```

### Function Signature:
Write a function f(N, A, connections) that takes in the input. 
def f(N: int, A: List[int], connections: List[Tuple[int, int]]): 
    ''' 
    N: an integer 
    A: a list of integers
    connections: a list of tuples
    '''

Test Case Generator

Certainly! To create a test case generator for the given problem, we need to generate:

1. The number of devices \(N\).
2. The list \(A\) of size \(N\), where each element \(A[i]\) represents the power consumption or supply of the \(i\)-th device.
3. A list of \(N-1\) unique cables connecting the devices, represented as pairs \((U_i, V_i)\).

Here's a Python function that generates random test cases for the problem:

```python
import random

def generate_test_case():
    N = random.randint(1, 5000)  # Number of devices
    A = [random.choice([-1, 1]) * random.randint(1, 10**9) for _ in range(N)]  # Power supply/consumption for each device

    # Generate a random tree structure
    edges = []
    if N > 1:
        nodes = list(range(1, N + 1))
        random.shuffle(nodes)
        for i in range(1, N):
            U = nodes[i]
            V = nodes[random.randint(0, i - 1)]
            edges.append((U, V))

    # Formatting the output
    test_case = f"{N}\n"
    test_case += ' '.join(map(str, A)) + '\n'
    for (U, V) in edges:
        test_case += f"{U} {V}\n"

    return test_case

# Example usage
print(generate_test_case())
```

This function does the following:
1. It generates a random number \(N\) within the given constraints.
2. It generates a list \(A\) of size \(N\) where each element is a random non-zero integer between \(-10^9\) and \(10^9\).
3. It generates \(N-1\) edges ensuring they form a valid tree structure by connecting nodes in a way that each node is connected to some previous node, ensuring all nodes are connected.

You can call `generate_test_case()` to get a new test case each time. This function ensures the constraints of the problem are met and generates valid random test cases for the problem.

Extract Arguments

def extract_arguments(fh):
    N = int(fh.readline().strip())
    A = list(map(int, fh.readline().strip().split()))
    cables = []
    for _ in range(N - 1):
        U, V = map(int, fh.readline().strip().split())
        cables.append((U, V))
    return N, A, cables