Proving Equivalences

Time limit2sMemory limit128 MB

Summary
Given a directed graph of proven implications, find the minimum number of edges to add so every node becomes mutually reachable (classic SCC condensation + max(sources, sinks) technique).
Level

Medium6 of 10

Topics
Graph, Union-find, Greedy
Solved
No attempts yet

Problem

The great mathematician Kim Seon-yeong, while writing a linear algebra textbook, came up with the following problem.

For an N×NN \times N matrix AA, prove that the following statements are equivalent.

  1. AA is invertible.
  2. For every N×1N \times 1 matrix bb, Ax=bAx = b has a unique solution.
  3. For every N×1N \times 1 matrix bb, Ax=bAx = b has a solution.
  4. Ax=0Ax = 0 has only the solution x=0x = 0.

A common way to solve such a problem is to use a chain of implications. For example, one may argue as follows.

(1) implies (2), (2) implies (3), (3) implies (4), and finally (4) implies (1). These four implications show that all four statements are equivalent.

Another way is to prove that (1) implies (2) and (2) implies (1), so that (1) and (2) are equivalent, and likewise prove that (2) and (3) are equivalent, and that (3) and (4) are equivalent. However, this needs as many as six implications.

Since Kim Seon-yeong must prove countless statements equivalent while writing the textbook, such inefficiency is fatal. Let us help prove equivalences using as few implications as possible.

In general, you are given nn statements and mm already-proven implications. Each implication means "if statement s1s_1 is true, then statement s2s_2 is also true." Find the minimum number of additional implications that must be proven so that all given statements become equivalent (that is, if any one of them is true, all the others are true as well).

Input

The first line contains the number of test cases TT (1≤T≤1001 \le T \le 100).

Each test case is given as follows.

  • The first line contains the number of statements nn (1≤n≤200001 \le n \le 20000) and the number of already-proven implications mm (0≤m≤500000 \le m \le 50000), separated by a space.
  • Each of the next mm lines contains two integers s1s_1 and s2s_2 (1≤s1,s2≤n1 \le s_1, s_2 \le n, s1≠s2s_1 \ne s_2), denoting an already-proven implication "if statement s1s_1 is true, then statement s2s_2 is also true."

Output

For each test case, print a single integer on its own line.

Print the minimum number of additional implications that must be proven so that all given statements become equivalent.

Examples6

  1. Example 1

    Input
    2
    4 0
    3 2
    1 2
    1 3
    
    Expected output
    4
    2
    
  2. Example 2

    Input
    1
    1 0
    
    Expected output
    0
    
  3. Example 3

    Input
    1
    3 3
    1 2
    2 3
    3 1
    
    Expected output
    0
    
  4. Example 4

    Input
    1
    4 3
    1 2
    2 3
    3 4
    
    Expected output
    1
    
  5. Example 5

    Input
    1
    2 0
    
    Expected output
    2
    
  6. Example 6

    Input
    1
    5 4
    1 3
    1 4
    2 4
    2 5
    
    Expected output
    3