Minimizing Edges
Bessie has a connected, undirected graph G with N vertices labeled 1…N and M edges (2≤N≤105,N−1≤M≤2N2+N). G may contain self-loops (edges from nodes back to themselves), but no parallel edges (multiple edges connecting the same endpoints).
Let fG(a,b) be a boolean function that evaluates to true if there exists a path from vertex 1 to vertex a that traverses exactly b edges for each 1≤a≤N and 0≤b, and false otherwise. If an edge is traversed multiple times, it is included that many times in the count.
Elsie wants to copy Bessie. In particular, she wants to construct an undirected graph G′ such that fG′(a,b)=fG(a,b) for all a and b.
Elsie wants to do the least possible amount of work, so she wants to construct the smallest possible graph. Therefore, your job is to compute the minimum possible number of edges in G′.
Each input contains T (1≤T≤5⋅104) test cases that should be solved independently. It is guaranteed that the sum of N over all test cases does not exceed 105, and the sum of M over all test cases does not exceed 2⋅105.
In the first test case, Elsie can construct G′ by starting with G and removing (2,5). Or she could construct a graph with the following edges, since she isn't restricted to just removing edges from G:
1 2 1 4 4 3 4 5
Elsie definitely cannot do better than N−1 since G′ must also be connected.
In each of these test cases, Elsie cannot do better than Bessie.
SCORING:
- All test cases in input 3 satisfy N≤5.
- All test cases in inputs 4-5 satisfy M=N.
- For all test cases in inputs 6-9, if it is not the case that fG(x,b)=fG(y,b) for all b, then there exists b such that fG(x,b) is true and fG(y,b) is false.
- All test cases in inputs 10-15 satisfy N≤102.
- Test cases in inputs 16-20 satisfy no additional constraints.
Problem credits: Benjamin Qi
Source: USA Computing Olympiad.
Input Format
The first line of the input contains T, the number of test cases.
The first line of each test case contains two integers N and M.
The next M lines of each test case each contain two integers x and y (1≤x≤y≤N), denoting that there exists an edge between x and y in G.
Consecutive test cases are separated by newlines for readability.
Output Format
For each test case, the minimum possible number of edges in G′ on a new line.