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缩点,之后对于一张DAG,找入度和出度等于0的点,最大值就是答案。
注意当原图就是一个强连通分量时,输出0.
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<stack>
using namespace std;
const int N=20005;
const int M=50005;
int T,n,m,cnt,dcnt,tim,x[M],y[M],hd[N],low[N],dfn[N],belong[N],ru[N],chu[N];
bool ins[N];
stack<int>stk;
struct edge
{
int to,nxt;
}v[M];
void addedge(int x,int y)
{
v[++cnt].to=y;
v[cnt].nxt=hd[x];
hd[x]=cnt;
}
void tarjan(int u)
{
dfn[u]=low[u]=++tim;
stk.push(u);
ins[u]=1;
for(int i=hd[u];i;i=v[i].nxt)
if(!dfn[v[i].to])
{
tarjan(v[i].to);
low[u]=min(low[u],low[v[i].to]);
}
else if(ins[v[i].to])
low[u]=min(low[u],dfn[v[i].to]);
if(dfn[u]==low[u])
{
++dcnt;
while(1)
{
int t=stk.top();
stk.pop();
belong[t]=dcnt;
ins[t]=0;
if(u==t)
break;
}
}
}
int main()
{
scanf("%d",&T);
while(T--)
{
cnt=tim=dcnt=0;
memset(hd,0,sizeof(hd));
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
memset(ru,0,sizeof(ru));
memset(chu,0,sizeof(chu));
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
scanf("%d%d",&x[i],&y[i]);
addedge(x[i],y[i]);
}
for(int i=1;i<=n;i++)
if(!dfn[i])
tarjan(i);
if(dcnt==1)
{
printf("0\n");
continue;
}
for(int i=1;i<=m;i++)
if(belong[x[i]]!=belong[y[i]])
chu[belong[x[i]]]++,ru[belong[y[i]]]++;
int a=0,b=0;
for(int i=1;i<=dcnt;i++)
{
if(!ru[i])
a++;
if(!chu[i])
b++;
}
printf("%d\n",max(a,b));
}
return 0;
}