#P14121. [SCCPC 2021] Don't Really Like How The Story Ends
[SCCPC 2021] Don't Really Like How The Story Ends
题目描述
There are planets in the galaxy, and many undirected warp tunnels connecting them. years ago, Spinel performed a depth-first search on the planets, visited all of them, and labeled them from to in the order of discovery.
Many warp tunnels have broken down since, and only of them are still working. Spinel wants to know how many new warp tunnels have to be built so that it is possible to perform a depth-first search, where the order of discovery is exactly as labeled years ago.
Recall that the depth-first search (DFS) algorithm inputs a graph and a vertex of , and labels all vertices reachable from as discovered.
Here is the pseudocode of a recursive implementation of DFS:
procedure DFS(G, v) is
label v as discovered
for all vertices w that there exists an edge between v and w do
if vertex w is not labeled as discovered then
recursively call DFS(G, w)
输入格式
There are multiple test cases. The first line of the input contains an integer indicating the number of test cases. For each test case:
The first line contains two integers and () indicating the number of planets and the number of remaining warp tunnels.
For the following lines, the -th line contains two integers and () indicating a warp tunnel between and .
It's guaranteed that the sum of of all test cases will not exceed .
输出格式
For each test case output one line containing one integer, indicating the minimum number of new warp tunnels that have to be built.
3
2 3
1 1
1 2
2 1
4 1
1 4
4 2
1 2
3 4
0
2
1
提示
For the second sample test case we can add a tunnel between planet and , and add another tunnel between planet and .
For the third sample test case we can add a tunnel between planet and .