Proving Equivalences
Time limit2sMemory limit128 MB
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 matrix , prove that the following statements are equivalent.
- is invertible.
- For every matrix , has a unique solution.
- For every matrix , has a solution.
- has only the solution .
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 statements and already-proven implications. Each implication means "if statement is true, then statement 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 ().
Each test case is given as follows.
- The first line contains the number of statements () and the number of already-proven implications (), separated by a space.
- Each of the next lines contains two integers and (, ), denoting an already-proven implication "if statement is true, then statement 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.