杭电2767 Proving Equivalences(强连通)

时间:2022-09-26 09:47:35

Proving Equivalences

Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 2   Accepted Submission(s) : 1
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
/*
题意:说半天搞不懂的矩阵,就是求加最少的边是整个变成一个强连通图
注意特殊数据 n为1 和 缩点后cnt=1答案为0
Time:2014-8-17
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN=20000+20;
const int MAXM=50000+50;
struct Edge{
int to,next;
}edge[MAXM*2];
int N,M;
int edgeNum,Top,cnt,index;
int dfn[MAXN],low[MAXN],stack[MAXN],belong[MAXN];
bool instack[MAXN];
int head[MAXN],in[MAXN],out[MAXN];
void Add(int u,int v){
edge[edgeNum].to=v;
edge[edgeNum].next=head[u];
head[u]=edgeNum++;
}
void Init(){
cnt=index=Top=edgeNum=0;
memset(head,-1,sizeof(head));
memset(belong,0,sizeof(belong));
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
memset(in,0,sizeof(in));
memset(out,0,sizeof(out));
memset(stack,0,sizeof(stack));
memset(instack,0,sizeof(instack));
int a,b;
while(M--){
scanf("%d%d",&a,&b);
Add(a,b);
}
}
void Tarjan(int u){
dfn[u]=low[u]=++index;
stack[++Top]=u;
instack[u]=true;
for(int i=head[u];~i;i=edge[i].next){
int v=edge[i].to;
if(dfn[v]==0){
Tarjan(v);
low[u]=min(low[u],low[v]);
}else if(instack[v]){
low[u]=min(low[u],dfn[v]);
}
}
int v;
if(low[u]==dfn[u]){
cnt++;
do{
v=stack[Top--];
instack[v]=false;
belong[v]=cnt;
}while(u!=v);
}
}
void solve(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d%d",&N,&M);
Init();

for(int i=1;i<=N;i++){
if(dfn[i]==0)
Tarjan(i);
}
int v;
for(int i=1;i<=N;i++){
for(int j=head[i];~j;j=edge[j].next){
v=edge[j].to;
if(belong[i]!=belong[v]){
in[belong[v]]++;
out[belong[i]]++;
}
}
}
if(N==1){printf("0\n");continue;}//注意两组特殊数据
if(cnt==1){printf("0\n");continue;}
int In=0,Out=0;
for(int i=1;i<=cnt;i++){
if(in[i]==0)In++;
if(out[i]==0)Out++;
}
printf("%d\n",max(In,Out));
}
}
int main(){
solve();
return 0;
}