# 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;
}

• 本文已收录于以下专栏：

## HDOJ 2767 Proving Equivalences

• RaAlGhul
• 2016年04月11日 15:36
• 293

## hdoj--2767--Proving Equivalences （scc+缩点）

Proving Equivalences Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Oth...
• qq_29963431
• 2015年10月23日 20:38
• 179

## hdu 2767 Proving Equivalences (Kosaraju+缩点)

• qq7366020
• 2013年10月22日 18:13
• 877

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

HDU 2767 Proving Equivalences（强连通 Tarjan+缩点） ACM 题目地址：HDU 2767 题意：  给定一张有向图，问最少添加几条边使得有向图成...
• hcbbt
• 2014年07月30日 15:40
• 2282

## poj2186Popular Cows【scc+缩点】

Language: Default Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total S...
• R1986799047
• 2015年11月19日 11:45
• 248

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

Summer Holiday Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)...
• qq_29963431
• 2015年10月23日 17:49
• 178

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

Popular Cows Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) To...
• qq_29963431
• 2015年10月23日 21:27
• 176

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

Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 1406...
• qq_29963431
• 2016年01月12日 15:27
• 164

## hdoj1827Summer Holiday【scc+缩点】

Summer Holiday Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...
• R1986799047
• 2015年11月18日 22:21
• 238

## hdu2767 Proving Equivalences（缩点）

• hill_555
• 2013年07月31日 16:24
• 364

举报原因： 您举报文章：hdoj2767Proving Equivalences【scc+缩点】 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)