Orig Description
Score : 2200 points
Problem StatementYou are given an undirected graph with N vertices and M edges.
Here, N-1≤M≤N holds and the graph is connected.
There are no self-loops or multiple edges in this graph.
The vertices are numbered 1 through N, and the edges are numbered 1 through M.
Edge i connects vertices a_i and b_i.
The color of each vertex can be either white or black.
Initially, all the vertices are white.
Snuke is trying to turn all the vertices black by performing the following operation some number of times:
Select a pair of adjacent vertices with the same color, and invert the colors of those vertices. That is, if the vertices are both white, then turn them black, and vice versa.
Determine if it is possible to turn all the vertices black. If the answer is positive, find the minimum number of times the operation needs to be performed in order to achieve the objective.
Constraints
2≤N≤10^5
N-1≤M≤N
1≤a_i,b_i≤N
There are no self-loops or multiple edges in the given graph.
The given graph is connected.
Partial Score
In the test set worth 1500 points, M=N-1.
InputThe input is given from Standard Input in the following format:
N M
a_1 b_1
a_2 b_2
:
a_M b_M
OutputIf it is possible to turn all the vertices black, print the minimum number of times the operation needs to be performed in order to achieve the objective.
Otherwise, print -1 instead.
Sample Input 16 5
1 2
1 3
1 4
2 5
2 6
Sample Output 15
For example, it is possible to perform the operations as shown in the following diagram:
Sample Input 23 2
1 2
2 3
Sample Output 2-1
It is not possible to turn all the vertices black.
Sample Input 34 4
1 2
2 3
3 4
4 1
Sample Output 32
This case is not included in the test set for the partial score.
Sample Input 46 6
1 2
2 3
3 1
1 4
1 5
1 6
Sample Output 47
This case is not included in the test set for the partial score.
Extracted Specification
An integer N (2 ≤ N ≤ 10^5), representing some quantity or size.
An integer M (N-1 ≤ M ≤ N), representing some quantity or size.
A list of M pairs of integers (a_i, b_i) where each integer is between 1 and N.
### Example Input:
```
6
5
1 2
1 3
1 4
2 5
2 6
```
### Function Signature:
Write a function f(N, M, edges) that takes in the input.
def f(N: int, M: int, edges: List[Tuple[int, int]]):
'''
N: an integer
M: an integer
edges: a list of tuples, where each tuple contains two integers
'''
Test Case Generator
To generate a test case generator for the given problem statement, we need to create a generator that produces valid inputs for the problem. The graph must be connected, with the number of edges \( M \) between \( N-1 \) and \( N \), and there should be no self-loops or multiple edges.
Here's the test case generator in Python:
```python
import random
def generate_connected_graph(N, M):
from collections import defaultdict, deque
# Ensure we have a connected graph first using a tree
edges = []
for i in range(2, N+1):
edges.append((random.randint(1, i-1), i))
# If we need more edges, add some randomly
additional_edges_needed = M - (N - 1)
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 and (j, i) not in edges]
random.shuffle(possible_edges)
edges.extend(possible_edges[:additional_edges_needed])
random.shuffle(edges)
return edges
def tcgen():
N = random.randint(2, 10**5)
M = random.randint(N-1, N)
edges = generate_connected_graph(N, M)
input_str = f"{N} {M}\n"
for a, b in edges:
input_str += f"{a} {b}\n"
return input_str.strip()
# Generate a test case and print it
print(tcgen())
```
### Explanation:
1. **Generating a Connected Graph:**
- We first ensure the graph is connected by creating a tree structure (which is inherently connected). This is done by connecting each vertex \( i \) from 2 to \( N \) to a random vertex from 1 to \( i-1 \).
- If \( M > N-1 \), we add additional edges randomly to satisfy the condition \( N-1 \leq M \leq N \).
2. **Generating the Input String:**
- The input starts with \( N \) and \( M \).
- For each edge, we append the vertices \( a \) and \( b \) to the input string.
3. **Randomization:**
- We shuffle the possible additional edges to ensure randomness.
- We shuffle the final edge list to avoid any specific order in the output.
This test case generator will produce valid inputs for the given problem, ensuring all constraints are respected.
Extract Arguments
def extract_arguments(fh):
N, M = map(int, fh.readline().strip().split())
edges = []
for _ in range(M):
a, b = map(int, fh.readline().strip().split())
edges.append((a, b))
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)