Proving Equivalences 判断至少加几个边可以变成强联通图+很好的模板

时间:2021-11-10 11:59:01

Proving Equivalences

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2475    Accepted Submission(s): 950


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
 
 
24 03 21 21 3
 

Sample Output
 
 
42
 

Source
 

Recommend
lcy   |   We have carefully selected several similar problems for you:   2768  2766  2769  2773  2772 
 
 
 
 
这个题目其实Tarjan算法的模板,我在使用很多遍的情况下差不多学会了怎么使用Tarjan算法的模板,还是值得开心的。
 
这个题目是问至少加几个边可以使其凑成一个强联通图,我们要进行缩点,然后把
强联通的每个出度入度为0的个数,找出最大的,就是要找的几个边,我是在做了poj
的network 0f schools之后才做这个的,还是比较水的
 
基本是模板,其中B代表的是每个节点属于哪个强联通,很关键很重要
 
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define M 50010
#define N 20010
struct node
{
int v,next;

} unit[M];
int first[N],stack[N],DFN[N],Low[N],B[N];
int instack[N];
int in[N],out[N];
int n,m,c,sc,top,num;

void init()
{
c=0;
sc=top=num=0;
memset(first,-1,sizeof(first));
memset(B,0,sizeof(B));
memset(DFN,0,sizeof(DFN));
memset(out,0,sizeof(out));
memset(in,0,sizeof(in));
}
int min(int a, int b)
{
return a < b ? a : b;
}

int max(int a, int b)
{
return a > b ? a : b;
}
void add(int num,int a,int b)
{
unit[num].v=b;
unit[num].next=first[a];
first[a]=num++;
}

void Tarjan(int v)
{

int min,t,e,j;
DFN[v]=Low[v]=++num;
instack[v]=1;
stack[top++]=v;
for(e=first[v]; e!=-1; e=unit[e].next)
{
j=unit[e].v;
if(!DFN[j])
{
Tarjan(j);
if(Low[v]>Low[j])
{
Low[v]=Low[j];
}


}
else if(instack[j]&&DFN[j]<Low[v])
{
Low[v]=DFN[j];
}
}
if(DFN[v]==Low[v])
{
while(v!=stack[top])
{

B[stack[top-1]]=sc;

instack[stack[top-1]]=0;

top--;

}

sc++;
}







}
void solve()
{

int i;
for(i=1; i<=n; i++)
{
if(!DFN[i])
{
Tarjan(i);
}
}
}






int main()
{
int T;
scanf("%d",&T);
while(T--)
{


scanf("%d%d",&n,&m);

init();
int i,j;
for(i=0; i<m; i++)
{
int a,b;
scanf("%d%d",&a,&b);
add(i,a,b);
}
solve();
if(sc==1)
{
printf("0\n");
continue;
}
else
{

for(int i=1; i<=n; i++)
{
for(int end=first[i]; end!=-1; end=unit[end].next)
{
if(B[i]!=B[unit[end].v])
{

in[B[unit[end].v]]++;

out[B[i]]++;

}

}

}

int maxin=0,maxout=0;
// printf("ccc %d\n",sc);
/* for(i=0;i<sc;i++)
{
printf("%d %d\n",in[i],out[i]);
}*/
for(int i=0; i<sc; i++)

{

if(in[i]==0)maxout++;

if(out[i]==0)maxin++;

}

printf("%d\n",max(maxout,maxin));

}

}
return 0;
}