#P16312. [ICPC 2023 Jinan R] 我只想要…多一个…

[ICPC 2023 Jinan R] 我只想要…多一个…

Problem Description

After finishing the paper Sandpile Prediction on Structured Undirected Graphs, Xiaoqingyu wants everyone to solve more graph theory problems. “We cannot live without graph theory problems, and everyone should work on sandpile prediction problems!”

A bipartite graph is a graph that satisfies the following condition: its vertices can be divided into two disjoint sets UU and VV, such that every edge in the graph connects a vertex in UU and a vertex in VV. If the numbers of vertices in UU and VV are equal, then the graph is called a balanced bipartite graph.

A matching in an undirected graph is a set of edges in which no two edges share a common endpoint. A maximum matching is a matching that contains the largest number of edges. The matching number of a graph is the number of edges in a maximum matching of the graph.

Now, Xiaoqingyu gives you a balanced bipartite graph. You need to add exactly one edge, connecting one vertex in UU and one vertex in VV, so that the matching number of the graph increases. Find the number of ways.

Input Format

There are multiple test cases. The first line contains an integer TT indicating the number of test cases. For each test case:

The first line contains two integers nn and mm (1n,m1051 \le n,m \le 10^5), representing the number of vertices in UU and VV, and the number of edges.

In the next mm lines, the ii-th line contains two integers uiu_i and viv_i (1ui,vin1 \le u_i, v_i \le n), indicating that the ii-th edge connects the uiu_i-th vertex in UU and the viv_i-th vertex in VV. The graph may contain multiple edges.

It is guaranteed that the sum of (n+m)(n + m) over all test cases does not exceed 4×1054 \times 10^5.

Output Format

For each test case, output one line containing one integer, representing the answer.

3
4 3
1 2
3 2
4 3
3 3
1 3
2 2
3 1
3 2
1 2
1 2
6
0
4

Hint

For the first sample test case, the matching number of the original graph is 22. By adding the edge (1,1)(1, 1), (1,4)(1, 4), (2,1)(2, 1), (2,4)(2, 4), (3,1)(3, 1), or (3,4)(3, 4), we can increase the matching number to 33. So the answer is 66.

For the second sample test case, the matching number of the original graph is 33. Clearly, we cannot increase the matching number, because all vertices are already in the matching, so the answer is 00.

For the third sample test case, the matching number of the original graph is 11. By adding the edge (2,1)(2, 1), (2,3)(2, 3), (3,1)(3, 1), or (3,3)(3, 3), we can increase the matching number to 22. So the answer is 44.

Translated by ChatGPT 5