# Proving Equivalences

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4470    Accepted Submission(s): 1577

Problem Description
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:

1. A is invertible.
2. Ax = b has exactly one solution for every n × 1 matrix b.
3. Ax = b is consistent for every n × 1 matrix b.
4. 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 ≤ 20000) and m (0 ≤ m ≤ 50000): 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.

Sample Input
2 4 0 3 2 1 2 1 3

Sample Output
4 2

Source

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<stack>
#include<vector>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=100010;
int dfs_clock,scc_cnt;
int low[maxn];
int dfn[maxn];
int in[maxn];
int out[maxn];
int sccno[maxn];
bool instack[maxn];
stack<int>S;
vector<int>scc[maxn];
vector<int>G[maxn];
struct Node{
int from,to,next;
}A[maxn];
int MAX(int a,int b){
return a>b?a:b;
}
int MIN(int a,int b){
return a<b?a:b;
}
void init(){
dfs_clock=scc_cnt=0;
memset(low,0,sizeof(low));
memset(dfn,0,sizeof(dfn));
memset(in,0,sizeof(in));
memset(out,0,sizeof(out));
memset(instack,false,sizeof(instack));
memset(sccno,0,sizeof(sccno));
}
void tarjan(int u,int pre){
int v;
dfn[u]=low[u]=++dfs_clock;
instack[u]=true;S.push(u);
v=A[k].to;
if(!dfn[v]){
tarjan(v,u);
low[u]=MIN(low[u],low[v]);
}
else if(instack[v]){
low[u]=MIN(low[u],dfn[v]);
}
}
if(low[u]==dfn[u]){
scc_cnt++;scc[scc_cnt].clear();
G[scc_cnt].clear();
while(1){
v=S.top();S.pop();
instack[v]=false;
sccno[v]=scc_cnt;
scc[scc_cnt].push_back(v);
if(u==v)break;
}
}
}
void suodian(int m){
for(int i=0;i<m;++i){
int u=sccno[A[i].from];
int v=sccno[A[i].to];
if(u!=v){
G[u].push_back(v);
in[v]++;out[u]++;
}
}
}
int main()
{
int i,j,k,n,m,t;
scanf("%d",&t);
while(t--){
init();
scanf("%d%d",&n,&m);
for(i=0;i<m;++i){
scanf("%d%d",&A[i].from,&A[i].to);
}
for(i=1;i<=n;++i){
if(!dfn[i]){
tarjan(i,-1);
}
}
if(scc_cnt==1){
printf("%d\n",0);
continue;
}
suodian(m);
int ans1=0,ans2=0;
for(i=1;i<=scc_cnt;++i){
if(in[i]==0)ans1++;
if(out[i]==0)ans2++;
}
printf("%d\n",MAX(ans1,ans2));
}
return 0;
}

#### HDU 2767-Proving Equivalences(强联通+缩点)

2015-07-01 17:24:40

#### 不是图论 【tarjan求scc】+【缩点】

2017-04-30 14:39:40

#### poj--1236--Network of Schools（scc+缩点）

2016-01-12 15:27:15

#### hdoj1827Summer Holiday【scc+缩点】

2015-11-18 22:21:24

#### poj--2186--Popular Cows (scc+缩点）

2015-10-23 21:27:24

#### poj2186Popular Cows【scc+缩点】

2015-11-19 11:45:08

#### hdoj-1827-Summer Holiday(scc+缩点)

2015-10-23 17:49:47

#### HDU 2767 Proving Equivalences（强连通 Tarjan+缩点）

2014-07-30 15:40:46

#### hdu2767Proving Equivalences(强连通+缩点)

2013-07-20 10:03:36

#### hdoj--3072--Intelligence System(scc+缩点+数据去重)

2015-10-24 11:37:13