#P7417. [USACO21FEB] Minimizing Edges P

[USACO21FEB] Minimizing Edges P

Description

Bessie has a connected undirected graph GG. GG has NN nodes numbered 1N1\ldots N and MM edges (2N105,N1MN2+N22\le N\le 10^5, N-1\le M\le \frac{N^2+N}{2}). GG may contain self-loops (an edge that connects a node to itself), but it does not contain multiple edges (multiple edges connecting the same pair of nodes).

Let fG(a,b)f_G(a,b) be a boolean function. For each 1aN1\le a\le N and 0b0\le b, the function is true if there exists a path from node 11 to node aa that uses exactly bb edges, and false otherwise. If an edge is traversed multiple times, then it is counted that many times.

Elsie wants to copy Bessie. Specifically, she wants to construct an undirected graph GG' such that for all aa and bb, we have fG(a,b)=fG(a,b)f_{G'}(a,b)=f_G(a,b).

Elsie wants to do as little work as possible, so she wants to construct the smallest possible graph. Therefore, your task is to compute the minimum possible number of edges in GG'.

Each input contains TT (1T51041\le T\le 5\cdot 10^4) independent test cases. It is guaranteed that the sum of NN over all test cases does not exceed 10510^5, and the sum of MM over all test cases does not exceed 21052\cdot 10^5.

Input Format

The first line of the input contains TT, the number of test cases.

The first line of each test case contains two integers NN and MM.

Each of the next MM lines of a test case contains two integers xx and yy (1xyN1\le x\le y\le N), indicating that there is an edge connecting xx and yy in GG.

For readability, adjacent test cases are separated by a blank line.

Output Format

For each test case, output one line, the minimum possible number of edges in GG'.

2

5 5
1 2
2 3
2 5
1 4
4 5

5 5
1 2
2 3
3 4
4 5
1 5
4
5
7

8 10
1 2
1 3
1 4
1 5
2 6
3 7
4 8
5 8
6 7
8 8

10 11
1 2
1 5
1 6
2 3
3 4
4 5
4 10
6 7
7 8
8 9
9 9

13 15
1 2
1 5
1 6
2 3
3 4
4 5
6 7
7 8
7 11
8 9
9 10
10 11
11 12
11 13
12 13

16 18
1 2
1 7
1 8
2 3
3 4
4 5
5 6
6 7
8 9
9 10
9 15
9 16
10 11
11 12
12 13
13 14
14 15
14 16

21 22
1 2
1 9
1 12
2 3
3 4
4 5
5 6
6 7
7 8
7 11
8 9
8 10
12 13
13 14
13 21
14 15
15 16
16 17
17 18
18 19
19 20
20 21

20 26
1 2
1 5
1 6
2 3
3 4
4 5
4 7
6 8
8 9
8 11
8 12
8 13
8 14
8 15
8 16
8 17
9 10
10 18
11 18
12 19
13 20
14 20
15 20
16 20
17 20
19 20

24 31
1 2
1 7
1 8
2 3
3 4
4 5
5 6
6 7
6 9
8 10
10 11
10 16
10 17
10 18
10 19
10 20
11 12
12 13
13 14
14 15
15 16
15 17
15 18
15 19
15 20
15 21
15 22
15 23
15 24
21 22
23 24
10
11
15
18
22
26
31

Hint

Sample 1 Explanation

In the first test case, Elsie can construct GG' by removing (2,5)(2,5) from GG. Alternatively, she can also construct a graph containing the following edges, because she is not restricted to only removing edges from GG:

1 2
1 4
4 3
4 5

Elsie clearly cannot do better than N1N-1, because GG' must also be connected.

Sample 2 Explanation

In the above test cases, Elsie cannot do better than Bessie.

Subtask Properties

  • For another 5%5\% of the data, N5N\le 5.
  • For another 10%10\% of the data, M=NM=N.
  • For another 20%20\% of the data, if it is not true that fG(x,b)=fG(y,b)f_G(x,b)=f_G(y,b) for all bb, then there exists a bb such that fG(x,b)f_G(x,b) is true and fG(y,b)f_G(y,b) is false.
  • For another 30%30\% of the data, N102N\le 10^2.
  • For another 25%25\% of the data, there are no additional constraints.

Problem by: Benjamin Qi.

Translated by ChatGPT 5