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 tuples where each tuple consists of two integers a_i and b_i (1 ≤ a_i, b_i ≤ 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, each containing two integers
'''
Test Case Generator
Sure, I can help you write a test case generator for this problem. The generator will need to create a connected graph with \(N\) vertices and \(M\) edges where \(N-1 \leq M \leq N\). Here's a Python function that generates such test cases:
```python
import random
def tcgen():
N = random.randint(2, 10**5)
M = random.randint(N-1, N)
edges = []
# To ensure the graph is connected, we start by building a spanning tree
for i in range(2, N+1):
edges.append((random.randint(1, i-1), i))
# If more edges are needed, we add them randomly
additional_edges_needed = M - (N - 1)
while additional_edges_needed > 0:
u = random.randint(1, N)
v = random.randint(1, N)
if u != v and (u, v) not in edges and (v, u) not in edges:
edges.append((u, v))
additional_edges_needed -= 1
random.shuffle(edges)
return N, M, edges
# Generating a test case
N, M, edges = tcgen()
print(N, M)
for edge in edges:
print(edge[0], edge[1])
```
This function ensures that:
1. \(N\) is a random number between 2 and \(10^5\).
2. \(M\) is a random number between \(N-1\) and \(N\).
3. The edges are generated such that the graph is always connected. Initially, a spanning tree is created to ensure connectivity. If more edges are required, they are added randomly while avoiding self-loops and multiple edges.
Here is a step-by-step explanation of the code:
1. The function `tcgen()` generates random values for \(N\) and \(M\).
2. It initially creates a spanning tree to ensure the graph is connected by connecting each vertex \(i\) (where \(i\) ranges from 2 to \(N\)) to a random previous vertex.
3. If \(M > N-1\), additional edges are added randomly between any two different vertices until the total number of edges is \(M\).
4. The edges are then shuffled to avoid any pattern that might bias the test cases.
5. The function returns \(N\), \(M\), and the list of edges.
With this generator, you can easily create multiple test cases to validate your solution for the given problem.
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