HDU 2767 Proving Equivalences targan+缩点

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的点的数目,两点数目的最大值就是题目答案。。

为啥呢, 因为一条边只能增加一个点的入度或者出度。 。。

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
const int maxn=20005;
int ca;
int n,m;
int low[maxn],dfn[maxn],vis[maxn],ccnc[maxn];
int ans,ci,tot;
int in[maxn],on[maxn];
int insum,onsum;
vector<int>ve[maxn];
stack<int>s;
void init()
{
    ans=tot=ci=0;
    insum=onsum=0;
    memset (dfn,0,sizeof(dfn));
    memset (low,0,sizeof(low));
    memset (ccnc,0,sizeof(ccnc));
    memset (in,0,sizeof(in));
    memset (on,0,sizeof(on));
    while (!s.empty())
             s.pop();
    for (int i=0;i<=n;i++)
         ve[i].clear();
}
void targan (int x)
{
    low[x]=dfn[x]=++tot;
    s.push(x);
    for (int i=0;i<ve[x].size();i++)
    {
        int v=ve[x][i];
        if(!dfn[v])
        {
            targan(v);
            low[x]=min(low[x],low[v]);
        }
        else if(!ccnc[v])
            low[x]=min(low[x],dfn[v]);
    }
    if(low[x]==dfn[x])
    {
        ci++;
        while (1)
        {
            int now=s.top();
            s.pop();
            ccnc[now]=ci;
            if(now==x)
                break;
        }
    }
}
int main()
{
    scanf("%d",&ca);
    while (ca--)
    {
        scanf("%d%d",&n,&m);
        init();
        while (m--)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            ve[x].push_back(y);
        }
        for (int i=1;i<=n;i++)
            if(!dfn[i])
               targan(i);
        if(ci==1)
        {
            printf("0\n");
            continue;
        }
        for (int i=1;i<=n;i++)
            for (int j=0;j<ve[i].size();j++)
             {
                int v=ve[i][j];
                if(ccnc[i]!=ccnc[v])
                {
                    in[ccnc[v]]++;
                    on[ccnc[i]]++;
                }
             }
        for (int i=1;i<=ci;i++)
        {
            if(in[i]==0)
                insum++;
            if(on[i]==0)
                onsum++;
        }
        int sum=max(insum,onsum);
        printf("%d\n",sum);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41410799/article/details/82528982