#HDU 2767 (tarjan + principle)

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

Subject to the effect: Enter a directed graph, at least you need to add a few side, in order to make this figure becomes strong graph?

As shown (somewhat ugly ha)

 Take this figure, the left 1, 2, 3, 4 is a rear condensing point of view (condensing point goes without saying) To make a strong FIG communication, so that he does not become a ring it, because the only ring can strongly connected, otherwise the total of a little-degree or out-degree is 0, so that this point can not be reached or else other point is reached, then we make up like this flaw, we to the point of zero as a "tail", to the point of zero as a "head", connected end to end, we can optimally solve the problem, or very image of the thing, but if the degree of the number and the number of 0 to 0 is not the same country? Look at a map you know it!

Black lines represent the point of FIG shrinkage, the red line is new and not look red, the degree of this figure this time point 0 and 1 respectively 4, point out a degree of only 0 3. After thought, to strongly connected, to have out-degree or degrees plus side operates as 0 point, in fact, the head and tail is a reason, if the reverse construction of FIG words, original head is built FIG reverse the tail, the tail is that after the original construction of the reverse view of the head, so that the maximum number of take on the OK! !

AC Code:

#include<iostream>
#include<cstring>
#include<stack>
using namespace std;
const int maxn = 5e4 + 5;

struct node
{
    int v, next;
}e[maxn];
int dfn[maxn], low[maxn], suo[maxn], cnt, tot, scnt;
int head[maxn], in[maxn], out[maxn], n, m, T;
bool vis[maxn];
stack <int> st;
void init() {
    memset(e, 0, sizeof(e));
    memset(dfn, 0, sizeof(dfn));
    memset(suo, 0, sizeof(suo));
    memset(head, -1, sizeof(head));
    memset(low, 0, sizeof(low));
    memset(vis, 0, sizeof(vis));
    memset(in, 0, sizeof(in));
    memset(out, 0, sizeof(out));
    cnt = tot = scnt = 0;
}
void add (int from, int to) {
    e[++cnt].v = to;
    e[cnt].next = head[from];
    head[from] = cnt;
}
void tarjan(int x) {
    dfn[x] = low[x] = ++tot;
    vis[x] = 1;
    st.push(x);
    for (int i = head[x]; i != -1; i = e[i].next) {
        if (!dfn[e[i].v]) {
            tarjan(e[i].v);
            low[x] = min (low[x], low[e[i].v]);
        }
        else if (vis[e[i].v]) low[x] = min (low[x], dfn[e[i].v]);
    }
    if (dfn[x] == low[x]) {
        scnt++;
        int k;
        do {
            k = st.top();
            st.pop();
            vis[k] = 0;
            suo[k] = scnt;
        }
        while (k != x);
    }
}

int main()
{
    cin >> T;
    while (T--) {
        cin >> n >> m;
        if (!m) {cout << n << endl; continue;}
        init();
        for (int i = 0; i < m; i++) {
            int ui, vi;
            cin >> ui >> vi;
            add (ui, vi);
        }
        for (int i = 1; i <= n; i++) {
            if (!dfn[i]) tarjan(i);
        }
        if (scnt == 1) {cout << 0 << endl; continue;}
        for (int i = 1; i <= n; i++) {
            for (int j = head[i]; j != -1; j = e[j].next) {
                int u = suo[i];
                int v = suo[e[j].v];
                if (u != v) out[u]++, in[v]++;
            }
        }
        int sum1 = 0, sum2 = 0;
        for (int i = 1; i <= scnt; i++) {
            if (!in[i]) sum1++;
            if (!out[i]) sum2++;
        }
        cout << max (sum1, sum2) << endl;
    }
    return 0;
}

 

Guess you like

Origin blog.csdn.net/weixin_43851525/article/details/91437341