2079: [USACO 2021 February Contest, Platinum] Problem 2. Minimizing Edges

文件提交:无需freopen 内存限制:256 MB 时间限制:2.000 S
评测方式:普通裁判 命题人:
提交:1 解决:1

题目描述

Bessie has a connected, undirected graph $G$ with $N$ vertices labeled $1\ldots N$ and $M$ edges ($2\le N\le 10^5, N-1\le M\le \frac{N^2+N}{2}$). $G$ may contain self-loops (edges from nodes back to themselves), but no parallel edges (multiple edges connecting the same endpoints).

Let $f_G(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\le a\le N$ and $0\le 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 $f_{G'}(a,b)=f_G(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\le T\le 5\cdot 10^4$) test cases that should be solved independently. It is guaranteed that the sum of $N$ over all test cases does not exceed $10^5$, and the sum of $M$ over all test cases does not exceed $2\cdot 10^5$.  

输入

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\le x\le y\le N$), denoting that there exists an edge between $x$ and $y$ in $G$.

Consecutive test cases are separated by newlines for readability.

输出

For each test case, the minimum possible number of edges in $G'$ on a new line.

样例输入

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

提示

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.

来源/分类