Problem p03052 - Generation 1

Orig Description

Score : 1200 points
Problem StatementYou are given a simple connected undirected graph G consisting of N vertices and M edges.
The vertices are numbered 1 to N, and the edges are numbered 1 to M.
Edge i connects Vertex a_i and b_i bidirectionally.
It is guaranteed that the subgraph consisting of Vertex 1,2,\ldots,N and Edge 1,2,\ldots,N-1 is a spanning tree of G.
An allocation of weights to the edges is called a good allocation when the tree consisting of Vertex 1,2,\ldots,N and Edge 1,2,\ldots,N-1 is a minimum spanning tree of G.
There are M! ways to allocate the edges distinct integer weights between 1 and M.
For each good allocation among those, find the total weight of the edges in the minimum spanning tree, and print the sum of those total weights modulo 10^{9}+7.
Constraints
All values in input are integers.
2 \leq N \leq 20
N-1 \leq M \leq N(N-1)/2
1 \leq a_i, b_i \leq N
G does not have self-loops or multiple edges.
The subgraph consisting of Vertex 1,2,\ldots,N and Edge 1,2,\ldots,N-1 is a spanning tree of G.
InputInput is given from Standard Input in the following format:
N M
a_1 b_1
\vdots
a_M b_M
OutputPrint the answer.
Sample Input 13 3
1 2
2 3
1 3
Sample Output 16
An allocation is good only if Edge 3 has the weight 3. For these good allocations, the total weight of the edges in the minimum spanning tree is 3, and there are two good allocations, so the answer is 6.
Sample Input 24 4
1 2
3 2
3 4
1 3
Sample Output 250
Sample Input 315 28
10 7
5 9
2 13
2 14
6 1
5 12
2 10
3 9
10 15
11 12
12 6
2 12
12 8
4 10
15 3
13 14
1 15
15 12
4 14
1 7
5 11
7 13
9 10
2 7
1 9
5 6
12 14
5 2
Sample Output 3657573092
Print the sum of those total weights modulo 10^{9}+7.

Extracted Specification

Canonicalized Input Description:

An integer N (2 ≤ N ≤ 20), representing some quantity or size.
An integer M (N-1 ≤ M ≤ N(N-1)/2), representing some quantity or size.
M pairs of integers (a_i, b_i) (1 ≤ a_i, b_i ≤ N), representing connections between entities.

### Example Input:

```
3 3
1 2
2 3
1 3
```

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

Test Case Generator

Sure! Here is a test case generator for the given problem statement:

```python
import random

def tcgen():
    # Constraints
    N = random.randint(2, 20)
    M = random.randint(N-1, N*(N-1)//2)
    
    # Generate a spanning tree for vertices 1 to N
    edges = []
    for i in range(2, N+1):
        edges.append((random.randint(1, i-1), i))
    
    # Generate the rest of the edges to ensure we have a connected graph
    all_possible_edges = [(i, j) for i in range(1, N+1) for j in range(i+1, N+1) if (i, j) not in edges]
    while len(edges) < M:
        edge = random.choice(all_possible_edges)
        if edge not in edges:
            edges.append(edge)
            all_possible_edges.remove(edge)
    
    random.shuffle(edges)
    
    return N, M, edges

def format_tc(N, M, edges):
    test_case_str = f"{N} {M}\n"
    for a, b in edges:
        test_case_str += f"{a} {b}\n"
    return test_case_str.strip()

# Example usage:
N, M, edges = tcgen()
print(format_tc(N, M, edges))
```

This Python function `tcgen` generates a random test case according to the specified problem constraints:

1. `N` is chosen randomly between 2 and 20.
2. `M` is chosen randomly between \(N-1\) and \(\frac{N(N-1)}{2}\).
3. The function first generates a spanning tree by sequentially connecting nodes, ensuring that the first \(N-1\) edges form a tree.
4. It then adds random edges to ensure the total number of edges is `M` without creating self-loops or duplicate edges.
5. The `format_tc` function converts the generated data into the required input format.

You can print the test case to see the output and use it for testing your solution.

Extract Arguments

def extract_arguments(fh):
    N, M = map(int, fh.readline().strip().split())
    edges = [tuple(map(int, fh.readline().strip().split())) for _ in range(M)]
    return N, M, edges

# if __name__ == "__main__":
#     input_path = sys.argv[1]
#     with open(input_path, 'r') as fh: 
#     N, M, edges = extract_arguments(fh)
#     f(N, M, edges)