hdu 2767 Proving Equivalences【scc缩点+搭桥】

来源:互联网 发布:maven 打包java环境 编辑:程序博客网 时间:2024/05/22 01:29

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
题意:
给出m条有向边,求至少搭多少桥使其成为强连通分量。
思路:
肯定要Tarjan缩点,缩点之后搭桥,把入度为0和出度为0的分别统计,取最大值。这样就可已使所有点有进有出,做后构成一个强连通图。以后遇到这种题,先进行缩点,方便考虑。

#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>const int maxn = 100010;using namespace std;int head[maxn], in[maxn], out[maxn]; int stack[maxn]; bool instack[maxn];int belong[maxn]; int dfn[maxn];int low[maxn];int index;int top; int scc;int tot;struct Edge {    int to;    int next;}edge[maxn << 3];void add_edge(int u, int v) {    edge[tot].to = v;    edge[tot].next = head[u];    head[u] = tot++;}void tarjan(int u) {    int v;    low[u] = dfn[u] = ++index;    stack[top++] = u;    instack[u] = true;    for(int i = head[u]; i != -1; i = edge[i].next) {        v = edge[i].to;        if(!dfn[v]) {            tarjan(v);            low[u] = min(low[u], low[v]);        }        else if(instack[v] && low[u] > dfn[v]) {            low[u] = dfn[v];        }    }    if(low[u] == dfn[u]) {         scc++;        do {            v = stack[--top];            instack[v] = false;            belong[v] =  scc;        }        while(v != u);    }}void solve(int n) {    memset(dfn, 0, sizeof(dfn));    memset(instack, false, sizeof(instack));    index = scc = top = 0;    for(int i = 1; i <= n; i++) {         if(!dfn[i])            tarjan(i);    }    for(int i = 1; i <= n; i++) { //scc缩点           for(int j = head[i]; j != -1; j = edge[j].next) {              int v = edge[j].to;              if(belong[i] != belong[v]) {                  in[belong[v]] = 1;                out[belong[i]] = 1;            }         }     }    int ans2 = 0, ans1 = 0;      if(scc == 1) {        printf("0\n");        return;    }    for(int i = 1; i <= scc; i++) {          if(!in[i]) ans1++;           if(!out[i]) ans2++;    }    printf("%d\n", max(ans1, ans2));  }int main() {    int n, m, A, B, t;    scanf("%d", &t);    while(t--) {          memset(head, -1, sizeof(head));        memset(in, 0, sizeof(in));        memset(out, 0, sizeof(out));        tot = 0;        scanf("%d %d", &n, &m);        while(m--) {            scanf("%d %d", &A, &B);            add_edge(A, B);        }        solve(n);    }    return 0;}
阅读全文
1 0
原创粉丝点击