题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2767
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
题目翻译:
考虑下面的练习,在一般线性代数教科书中找到。
设A为N×N矩阵。证明以下陈述是等效的:
1。A是可逆的。
2。ax=b每N×1矩阵b只有一个解。
3。每个n×1矩阵b的ax=b是一致的。
4。ax=0只有平凡解x=0。
解决这类问题的典型方法是展示一系列含义。例如,可以继续显示(a)暗示(b),表示(b)暗示(c),表示(c)暗示(d),最后表示(d)暗示(a)。这四个含义表明这四个语句是等价的。
另一种方法是证明(a)等同于(b)(通过证明(a)意味着(b)和(b)意味着(a),(b)等同于(c),(c)等同于(d)。然而,这种方法需要证明六个含义,这显然比证明四个含义要多得多!
我被赋予了一些类似的任务,并且已经开始证明一些含义。现在我想知道,我还需要证明多少含义?你能帮我确定吗?
输入
在第一行,一个正数:测试用例的数量,最多100个。之后每个测试用例:
*一行包含两个整数n(1≤n≤20000)和m(0≤m≤50000):语句数和已经证明的含义数。
*m两个整数分别为s1和s2(1≤s1,s2≤n和s1≠s2)的行,表示已经证明语句s1表示语句s2。
输出
每个测试用例:
*为了证明所有语句都是等价的,需要证明的附加含义最少的一行。
很经典的一道强连通题,首先需要缩点,之后用到了入度为0和出度为0点的性质。
刘汝佳的白书里面也有这道题。
#include <iostream>
#include<vector>
#include<cstdio>
#include<stack>
using namespace std;
const int maxn=1e5+7;
int pre[maxn],low[maxn],sccno[maxn],dfs_clock,scc_cnt;
int in0[maxn],out0[maxn];
stack<int> s;
vector<int> G[maxn];
int n,m;
void dfs(int u){
pre[u]=low[u]=++dfs_clock;
s.push(u);
for(int i = 0;i<(int)G[u].size();++i){
int v=G[u][i];
if(!pre[v]){
dfs(v);
low[u]=min(low[u],low[v]);
}
else if(!sccno[v]){
low[u]=min(low[u],pre[v]);
}
}
if(low[u]==pre[u]){
scc_cnt++;
while(1){
int x=s.top();
s.pop();
sccno[x]=scc_cnt;
if(x==u) break;
}
}
}
void find_scc(int n){
dfs_clock=scc_cnt=0;
memset(pre,0,sizeof(pre));
memset(sccno,0,sizeof(pre));
for(int i = 0;i<n;++i)
if(!pre[i])
dfs(i);
}
int main(int argc, char** argv) {
int T;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
for(int i = 0;i<n;++i) G[i].clear();
for(int i = 0;i<m;++i){
int x,y;
scanf("%d%d",&x,&y);
x--,y--;
G[x].push_back(y);
}
find_scc(n);
printf("%d\n",scc_cnt);
for(int i = 1;i<=scc_cnt;++i) in0[i]=out0[i]=1;
for(int u = 0;u<n;++u){
for(int i = 0;i<(int)G[u].size();++i){
int v=G[u][i];
if(sccno[u]!=sccno[v]) out0[sccno[v]]=in0[sccno[u]]=0;
}
}
int a=0,b=0;
for(int i = 1;i<=scc_cnt;++i){
if(in0[i]) a++;
if(out0[i]) b++;
}
if(scc_cnt==1) printf("0\n");
else printf("%d\n",max(a,b));
}
return 0;
}