Proving Equivalences
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 7601 Accepted Submission(s): 2646
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?
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.
* 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.
* 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
思路:先用 Tarjan 把整个图中的强连通分量找出来
如果只有一个,那么就不用加边了。
否则,就去遍历每一条边,判断这条边是不是连接了两个强连通分量,用来记录每个强连通分量是否有出度和入度
只要每个强连通分量都有出度和入度,那么它们自然而然地也就会形成一个强连通量
所以要使他成为一个连通图,就需要给每个强连通分量补至少一条边,因为只要它每个分量都有出度或者每个分量都有入度就可以,像这个图一样:
但是统计出需要补的入度和出度之后,需要取大的那个值,就如下面这个图
它缺少一个入度,两个出度,但是只补一个入度是不够的,因为补了一个入度之后还会缺少一个出度,因为每条边只会增加一个入度和一个出度,所以需要找大的那个值。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<stack>
#include<algorithm>
using namespace std;
#define maxn 20020
#define mem(a,x) memset(a,x,sizeof(a))
#define inf 0x3f3f3f3f
struct edge{
int to,nxt;
}e[maxn * 4];
int n,m,cnt,head[maxn],dfn[maxn],low[maxn],vis[maxn],in[maxn],out[maxn];
int dep,top,num,pre[maxn];
stack<int>sta;
void add(int u,int v){
e[cnt].to = v;
e[cnt].nxt = head[u];
head[u] = cnt++;
}
void tarjan(int u){
dfn[u] = low[u] = ++dep;
sta.push(u);
vis[u] = 1;
for(int i = head[u];i != -1;i = e[i].nxt){
int v = e[i].to;
if(!dfn[v]){
tarjan(v);
low[u] = min(low[u],low[v]);
}else if(vis[v]){
low[u] = min(low[u],dfn[v]);
}
}
int v;
if(dfn[u] == low[u]){
num++;
do{
v = sta.top();
sta.pop();
pre[v] = num;
vis[v] = 0;
}while(u != v);
}
}
void init(){
cnt = 0;
mem(head,-1);
dep = top = num = 0;
mem(dfn,0);
mem(low,0);
mem(vis,0);
mem(pre,0);
mem(in,0);
mem(out,0);
}
int main(){
int t;
scanf("%d",&t);
while(t--){
scanf("%d %d",&n,&m);
if(n == 1){
printf("0\n");
}else if(m == 0){
printf("%d\n",n);
}else{
init();
int u,v;
while(m--){
scanf("%d %d",&u,&v);
add(u,v);
}
for(int i = 1;i <= n;i++){
if(!dfn[i])
tarjan(i);
}
if(num == 1)
printf("0\n");
else{
for(u = 1;u <= n;u++){
for(int i = head[u];i != -1;i = e[i].nxt){
v = e[i].to;
if(pre[u] != pre[v]){
out[pre[u]]++;
in[pre[v]]++;
}
}
}
int ans1 = 0,ans2 = 0;
for(int i = 1;i <= num;i++){
if(in[i] == 0)
ans1++;
if(out[i] == 0)
ans2++;
}
printf("%d\n",max(ans1,ans2));
}
}
}
return 0;
}