Problem p01870 - Generation 2

Orig Description

F: Relay
Problem Statement
There is a village named Biwako consisting of $N$ small islands floating on a lake. There are $N-1$ simple bridges in Biwako village. The islands are numbered from $0$ to $N-1$, and the bridges are numbered from $0$ to $N-2$. The $i$-th bridge connects the $i+1$-th island with the $p_i$-th island directly, and its length is $w_i$. The villagers can move between any two islands through several bridges.
A relay race is going to be held in Biwako village according to the proposal of a villager. However, since there are no loops in Biwako village, they cannot prepare a track. Therefore, they consider replacing only one bridge to create a loop. Find the maximum length of the loop that can be created by this operation.
Input
$N$
$p_0 \ w_0$
$\vdots$
$p_{n-2} \ w_{n-2}$
Constraints
$2 \leq N \leq 100000$
$0 \leq p_i \leq N-1$
$1 \leq w_i \leq 1000$
All values are integers.
All islands can be reached from any other island.
Output
Print the answer in one line.
Sample
The figure for each sample is as follows.
Sample Input 1
5
0 1
0 2
0 3
0 4
Sample Output 1
9
Sample Input 2
12
0 6
1 1
0 3
3 4
0 2
5 1
6 1
0 2
8 1
9 1
10 2
Sample Output 2
19
Sample Input 3
2
0 1
Sample Output 3
1

Extracted Specification

An integer N (2 ≤ N ≤ 100000), representing some quantity or size.
A list of tuples [(p_0, w_0), ..., (p_{N-2}, w_{N-2})] where each tuple contains:
    - p_i (0 ≤ p_i ≤ N-1), an integer
    - w_i (1 ≤ w_i ≤ 1000), an integer

### Example Input:

```
5
0 1
0 2
0 3
0 4
```

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

Test Case Generator

Sure! Here's a test case generator for the given problem statement "F: Relay":

```python
import random

def tcgen():
    N = random.randint(2, 100000)
    
    p = []
    w = []
    for i in range(N-1):
        p_i = random.randint(0, i)
        w_i = random.randint(1, 1000)
        p.append(p_i)
        w.append(w_i)
    
    return N, p, w

def print_test_case():
    N, p, w = tcgen()
    
    print(N)
    for i in range(N-1):
        print(f"{p[i]} {w[i]}")

print_test_case()
```

### Explanation

1. **`N = random.randint(2, 100000)`**: This line generates a random integer \( N \) between 2 and 100,000, which is the number of islands.

2. **Generating `p` and `w` arrays**:
    - **Loop `for i in range(N-1)`**: This loop iterates \( N-1 \) times to generate the `p` and `w` arrays.
    - **`p_i = random.randint(0, i)`**: This line selects a random parent island for the \( i+1 \)-th island. It ensures that `p_i` is a valid parent island index.
    - **`w_i = random.randint(1, 1000)`**: This line generates a random bridge length between 1 and 1,000.

3. **`print_test_case` function**:
    - Calls `tcgen()` to generate a random test case.
    - Prints the number of islands \( N \).
    - Prints each \( p_i \) and \( w_i \) in a loop.

This generator will produce random valid inputs for the problem statement, ensuring that the constraints are met. You can run the `print_test_case` function to generate and print a random test case.

Extract Arguments

def extract_arguments(fh):
    N = int(fh.readline().strip())
    bridges = []
    for _ in range(N - 1):
        p_i, w_i = map(int, fh.readline().strip().split())
        bridges.append((p_i, w_i))
    return N, bridges

# Example usage:
# if __name__ == "__main__":
#     with open("input.txt", "r") as fh:
#         N, bridges = extract_arguments(fh)
#         print(N, bridges)