la 4287 hdu 2767 tarjan强连通分量 模板题

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, s2n and s1s2) 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
The 2008 ACM Northwestern European Programming Contest
 
求强连通分量,注意,如果给的是个不连通图,则需要用ins[u]来判断回边指向的点是否为改点的祖先;另外需要注意的地方有:
1.求缩点后各点出入度时,指向改点或由改点指出的边不该再由改点指出或指向改点。。。
2.当给定图为强连通图时,ans=1,非0。
 
1A代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<vector>
#include<cstdlib>
#include<algorithm>

using namespace std;

#define LL long long
#define ULL unsigned long long
#define UINT unsigned int
#define MAX_INT 0x7fffffff
#define MAX_LL 0x7fffffffffffffff
#define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
#define MIN(X,Y) ((X) < (Y) ? (X) : (Y))

#define MAXN 22222
#define MAXM 111111

struct edge{
    int u, v, nxt;
}e[MAXM];
int h[MAXN], cc;
int dfn[MAXN], low[MAXN], s[MAXN];
int ins[MAXN];
int tsp, cnt, top, id[MAXN];

inline void add(int u, int v){
    e[cc]=(edge){u, v, h[u]};
    h[u]=cc++;
}

void tarjan(int u){
    dfn[u]=low[u]=++tsp;
    s[top++]=u;     ins[u]=1;
    int i, v;
    for(i=h[u]; i!=-1; i=e[i].nxt){
        v=e[i].v;
        if(!dfn[v]) tarjan(v), low[u]=MIN(low[u], low[v]);
        else if(ins[v]) low[u]=MIN(low[u], dfn[v]);
    }
    if(low[u]==dfn[u]){
        cnt++;
        do{
            v=s[--top];     ins[v]=0;
            id[v]=cnt;
        }while(v!=u);
    }
}

int in[MAXN], out[MAXN];
int solve(int n){
    int i,j;
    memset(dfn, 0, sizeof(dfn));
    memset(ins, 0, sizeof(ins));
    tsp=cnt=top=0;
    for(i=0; i<n; i++) if(!dfn[i])
        tarjan(i);
    int a=0, b=0;
    memset(in, 0, sizeof(in));
    memset(out, 0, sizeof(out));
    for(i=0; i<cc; i++){
        int u=id[e[i].u], v=id[e[i].v];
        if(u!=v) in[v]++,out[u]++;              //非点内‘弧’
    }
    for(i=1; i<=cnt; i++){
        if(!in[i]) a++;
        if(!out[i]) b++;
    }
    return ( cnt==1 ? 0: MAX(a, b) );
}

int main(){
    //freopen("C:\Users\Administrator\Desktop\in.txt","r",stdin);
    int T, n, m;
    scanf(" %d", &T);
    while(T--){
        int i, j, u, v;
        scanf(" %d %d", &n, &m);
        memset(h, -1, sizeof(h));       cc=0;
        for(i=0; i<m; i++){
            scanf(" %d %d",&u, &v); u--; v--;
            add(u, v);
        }
        int ans=solve(n);
        printf("%d
",ans);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/ramanujan/p/3302139.html