Proving Equivalences(强连通分量 + Tarjan)

Proving Equivalences

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


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

       题意:

       给出 T(<= 100) 组case。后每组 case 首先给出 n(1 ~ 2000) 和 m(0 ~ 5000),代表有 N 个命题 和 M 条关系,每条关系式给出连个数,a 和 b 代表 a -> b。现要求所以的命题全部等价,即 a <-> b,b <-> c…… 输出最小要添加多少条关系来满足要求。

 

       思路:

       强连通分量。强连通缩点后,max (入度为 0 的节点数, 出度为 0 的节点数)即为所求。注意当本身已经为强连通时,输出 0 。

 

       AC:

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <stack>

using namespace std;

const int NMAX = 20005;
const int EMAX = 50005 * 2;

int n, m;
vector<int> v[NMAX];

int scc_cnt, dfs_clock;
int pre[NMAX], low[NMAX], cmp[NMAX];
stack<int> s;

int out[NMAX], in[NMAX];

void dfs(int u) {
        pre[u] = low[u] = ++dfs_clock;
        s.push(u);

        for (int i = 0; i < v[u].size(); ++i) {
                if (!pre[ v[u][i] ]) {
                        dfs( v[u][i] );
                        low[u] = min(low[u], low[ v[u][i] ]);
                } else if (!cmp[ v[u][i] ]) {
                        low[u] = min(low[u], pre[ v[u][i] ]);
                }
        }

        if (pre[u] == low[u]) {
                ++scc_cnt;
                for( ;;){
                        int x = s.top(); s.pop();
                        cmp[x] = scc_cnt;
                        if (x == u) break;
                }
        }
}

void scc() {
        scc_cnt = dfs_clock = 0;
        memset(pre, 0, sizeof(pre));
        memset(cmp, 0, sizeof(cmp));

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

void make_scc() {
        for (int i = 1; i <= scc_cnt; ++i) {
            out[i] = in[i] = 0;
        }

        for (int i = 1; i <= n; ++i) {
                for (int j = 0; j < v[i].size(); ++j) {
                        if (cmp[i] != cmp[ v[i][j] ]) {
                                    ++in[ cmp[v[i][j]] ];
                                    ++out[ cmp[i] ];
                            }
                }
        }
}

int main() {
        int t;
        scanf("%d", &t);
        while (t--) {
                scanf("%d%d", &n, &m);

                for (int i = 1; i <= n; ++i) v[i].clear();

                while (m--) {
                        int f, t;
                        scanf("%d%d", &f, &t);
                        v[f].push_back(t);
                }

                scc();

                if (scc_cnt == 1) printf("0\n");
                else {
                        make_scc();

                        int o_n = 0, i_n = 0;
                        for (int i = 1; i <= scc_cnt; ++i) {
                                if (!out[i]) ++o_n;
                                if (!in[i]) ++i_n;
                        }

                        printf("%d\n", max(o_n, i_n));
                }

        }
}

 

 

 

你可能感兴趣的:(tar)