Proving Equivalences

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


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
 

题目大意:

                  题目意思就是给你一个无向图,要你求使这图成为连通图所需的最小加的边,连通图就是所有点都能相互到达

题目思路

                  首先我们很好想到的是如果图中某些点已经联通了就可以压缩成一个点,所以我们可以先用targan缩点,然后我们考虑连通图的一些性质,所有点的入度和出度没有没0的,这个性质对于这题有很大的帮助,然后我们试着去统计所有点的出入度为0的个数,应为有为0的情况我们就要去连边,而一个入度或出度为0的连接一个出度过入度为0的点时可以抵消两个0,所以我们可以让出入度中为0的少的去连接为0多的一方,这样就能保证边最少!


AC代码:

#include<cstring>
#include<cstdio>
#define min(x,y) (x<y?x:y)
const int maxn = 2e4+10;
const int maxm = 5e4+10;

struct st{
  int v,nex;
}edge[maxm];

int hed[maxn],vis[maxn],low[maxn],dfn[maxn],belon[maxn],stack[maxn];
int in[maxn],out[maxn];
int n,m,e,top,cnt,num,sum1,sum2;

void init(){
    memset(hed,-1,sizeof(hed));
    memset(vis,0,sizeof(vis));
    memset(dfn,0,sizeof(dfn));
    memset(in,0,sizeof(in));
    memset(out,0,sizeof(out));
    e=1;
    top=num=cnt=0;
}

void add(int u,int v){
    edge[e].v=v,edge[e].nex=hed[u],hed[u]=e++;
}

void targan(int u){     //缩点
    dfn[u]=low[u]=++num;
    stack[top++]=u;
    vis[u]=1;
    for(int i=hed[u];~i;i=edge[i].nex){
        int v = edge[i].v;
        if(!dfn[v]){
            targan(v);
            low[u]=min(low[u],low[v]);
        }else if(vis[v]){
            low[u]=min(low[u],dfn[v]);
        }
    }
    if(low[u]==dfn[u]){
        cnt++;
        int x;
        do{
            x=stack[--top];
            vis[x]=0;
            belon[x]=cnt;
        }while(x!=u);
    }
}

int main()
{
    int t;scanf("%d",&t);
    while(t--){
        init();
        scanf("%d%d",&n,&m);
        int mm = m;
        while(m--){
            int u,v;scanf("%d%d",&u,&v);
            add(u,v);
        }
        for(int i=1;i<=n;i++){
            if(!dfn[i])targan(i);
        }
        if(cnt==1){
            printf("0\n");
            continue;
        }
        for(int i=1;i<=n;i++){
            for(int j = hed[i];~j;j=edge[j].nex){
                int v = edge[j].v;
                if(belon[i]!=belon[v]){
                    out[belon[i]]=1;       //记录出入度的情况
                    in[belon[v]]=1;
                }
            }
        }
        sum1=sum2=0;
        for(int i=1;i<=cnt;i++){
            if(in[i]==0)sum1++;    //分别统计出入度为0的个数
            if(out[i]==0)sum2++;
        }
        if(sum1<sum2)sum1=sum2;     //答案就是为0的大的一方
        printf("%d\n",sum1);
    }

    return 0;
}