PMATRIX - Proving Equivalences

Consider the following exercise, found in a generic linear algebra textbook. Let A be an n × n matrix. Prove that the following statements are equivalent:

  • (a) A is invertible.
  • (b) Ax = b has exactly one solution for every n × 1 matrix b.
  • (c) Ax = b is consistent for every n × 1 matrix b.
  • (d) Ax = 0 has only the trivial solution x = 0.

The typical way to solve such an exercise is to show a series of implications. For instance, one can proceed by showing that (a) implies (b), that (b) implies (c), that (c) implies (d),and finally that (d) implies (a). These four implications show that the four statements are equivalent. Another way would be to show that (a) is equivalent to (b) (by proving that (a) implies (b) and that (b) implies (a)), that (b) is equivalent to (c), and that (c) is equivalent to (d).

However, this way requires proving six implications, which is clearly a lot more work than just proving four implications! I have been given some similar tasks, and have already started proving some implications. Now I wonder, how many more implications do I have to prove? Can you help me determine this ?

Input

On the first line one positive number: the number of testcases, at most 100. After that per testcase:

  • One line containing two integers n (1 ≤ n ≤ 20 000) and m (0 ≤ m ≤ 50 000): the number of statements and the number of implications that have already been proved.
  • m lines with two integers s1 and s2 (1 ≤ s1, s2 ≤ n and s1 = s2) each, indicating that it has been proved that statement s1 implies statement s2.

Output

Per testcase:
One line with the minimum number of additional implications that need to be proved in order to prove that all statements are equivalent.

Example

Input:
2
4 0
3 2
1 2
1 3

Output:
4
2

Added by:Hemant Verma
Date:2009-07-25
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:ADA95 ASM32 BASH BF C CSHARP CPP C99 CLPS LISP sbcl LISP clisp D FORTRAN HASK ICON ICK JAVA LUA NEM NICE OCAML PAS-GPC PAS-FPC PERL PHP PIKE PRLG-swi PYTHON RUBY SCM guile SCM qobi ST TEXT WHITESPACE
Resource:NWERC 2008

hide comments
2016-09-13 10:54:52
please correct: "and s1 = s2" should be "and s1 != s2"
2015-05-16 17:25:41 _sanghk11_
WA

Last edit: 2015-05-17 16:26:19
2015-04-06 23:46:38 krish
Very good question:)
Learnt Something new..
2009-07-30 12:46:37 Hemant Verma
Time Limit Changed to 3 SEC

Last edit: 2009-11-15 10:07:36
2009-07-29 12:18:39 amaroq
I agree. A time limit of 3 or even 5 seconds is more appropriate, since just reading the input with scanf already takes around 0.5 seconds. I strongly doubt that any super-linear algorithm would pass within say 20 seconds (unless you're Eigenray, of course...).
2009-07-28 11:41:28 Seckin Can Sahin
time limit is too strict, think twice before implementing something.

edit: since the author revealed the problem a little, let me give you more insight: I implemented Tarjan's algo and also Kosaraju before that. Tarjan passed with 0.94 :)

Last edit: 2009-07-31 09:33:47
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.