#P14121. [SCCPC 2021] Don't Really Like How The Story Ends

[SCCPC 2021] Don't Really Like How The Story Ends

题目描述

There are nn planets in the galaxy, and many undirected warp tunnels connecting them. 60006000 years ago, Spinel performed a depth-first search on the planets, visited all of them, and labeled them from 11 to nn in the order of discovery.

Many warp tunnels have broken down since, and only mm 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 60006000 years ago.

Recall that the depth-first search (DFS) algorithm inputs a graph G\textit{G} and a vertex v\textit{v} of G\textit{G}, and labels all vertices reachable from v\textit{v} 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 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) indicating the number of planets and the number of remaining warp tunnels.

For the following mm lines, the ii-th line contains two integers uiu_i and viv_i (1ui,vin1 \le u_i,v_i \le n) indicating a warp tunnel between uiu_i and viv_i.

It's guaranteed that the sum of (n+m)(n + m) of all test cases will not exceed 10610^6.

输出格式

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 11 and 22, and add another tunnel between planet 22 and 33.

For the third sample test case we can add a tunnel between planet 22 and 33.