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
Canonicalized Input Description:
An integer N (2 ≤ N ≤ 100000), representing some quantity or size.
A list of N-1 tuples, where each tuple contains:
- An integer (0 ≤ value ≤ N-1)
- An integer (1 ≤ value ≤ 1000)
### Example Input:
```
5
0 1
0 2
0 3
0 4
```
### Function Signature:
Write a function f(N, inputs) that takes in the input.
```python
def f(N: int, inputs: List[Tuple[int, int]]):
'''
N: an integer
inputs: a list of tuples, where each tuple contains two integers
'''
```
Test Case Generator
Sure, I can help you write a test case generator for this problem. Let's break down the requirements and construct a generator step-by-step:
### Problem Analysis
- **Input:**
1. An integer \(N\) (2 ≤ \(N\) ≤ 100,000), representing the number of islands.
2. For each bridge (from 0 to \(N-2\)):
- \(p_i\) (0 ≤ \(p_i\) ≤ \(N-1\)): The island to which the \(i+1\)-th island is directly connected.
- \(w_i\) (1 ≤ \(w_i\) ≤ 1000): The length of the \(i\)-th bridge.
### Test Case Generator
The test case generator should:
1. Randomly choose \(N\) within the specified limits.
2. Generate \(N-1\) pairs of \(p_i\) and \(w_i\) satisfying the constraints.
We must ensure that all islands can be reached from any other, meaning we need to generate a connected acyclic graph (a tree).
Here’s the Python code for the test case generator:
```python
import random
def generate_test_case():
# Generate a random N within the given range
N = random.randint(2, 100000)
# Generate the bridges and their lengths
bridges = []
for i in range(1, N):
p_i = random.randint(0, i-1)
w_i = random.randint(1, 1000)
bridges.append((p_i, w_i))
# Format the test case as specified
test_input = [str(N)]
test_input.extend([f"{p} {w}" for p, w in bridges])
return "\n".join(test_input)
# Example of generating a test case and printing it
if __name__ == "__main__":
print(generate_test_case())
```
### Explanation of the Code
1. **Randomly Select \(N\):**
```python
N = random.randint(2, 100000)
```
This line selects a random integer \(N\) within the given constraints.
2. **Generate Bridges:**
```python
for i in range(1, N):
p_i = random.randint(0, i-1)
w_i = random.randint(1, 1000)
bridges.append((p_i, w_i))
```
This loop generates \(N-1\) bridges. Each bridge connects the \(i+1\)-th island to some previously generated island \(p_i\) and assigns a random length \(w_i\).
3. **Format Output:**
```python
test_input = [str(N)]
test_input.extend([f"{p} {w}" for p, w in bridges])
return "\n".join(test_input)
```
This part formats the generated data into the required input format.
### Example Usage
You can run this script to generate a test case and print it. This will provide you with the input for the problem, which you can then use to test your solution.
Feel free to modify the script to generate specific types of test cases, such as edge cases or cases that test the boundaries of the constraints.
Extract Arguments
def extract_arguments(fh):
N = int(fh.readline().strip())
bridges = []
for _ in range(N - 1):
p, w = map(int, fh.readline().strip().split())
bridges.append((p, w))
return N, bridges