HDU - 2767 Proving Equivalences【Tarjan】

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/niiick/article/details/82892227

Time limit 2000 ms
Memory limit 32768 kB

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 ©, that © 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 ©, and that © 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.


题目分析

题目等价于给定一个有向图
求至少加入几条边使得所有点出度入度都不为0

先Tarjan求强连通分量+缩点
如果求得只有一个强连通分量,那么可以特判直接输出0
否则在缩点后的图上分别记录出度为0和入度为0的点的个数
输出其中的较大值


#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<cstring>
#include<cstdio>
using namespace std;
typedef long long lt;

lt read()
{
    lt f=1,x=0;
    char ss=getchar();
    while(ss<'0'||ss>'9'){if(ss=='-')f=-1;ss=getchar();}
    while(ss>='0'&&ss<='9'){x=x*10+ss-'0';ss=getchar();}
    return f*x;
}

const int maxn=30010;
int t,n,m;
struct node{int v,nxt;}E[maxn<<2];
int head[maxn],tot;
int dfn[maxn],low[maxn],cnt;
int col[maxn],colnum;
int st[maxn],ins[maxn],top;
int in[maxn],out[maxn];

void add(int u,int v)
{
    E[++tot].nxt=head[u];
    E[tot].v=v;
    head[u]=tot;
}

void init()
{
    tot=cnt=colnum=0;
    memset(head,0,sizeof(head)); 
    memset(dfn,0,sizeof(dfn));
    memset(col,0,sizeof(col));
    memset(in,0,sizeof(in));
    memset(out,0,sizeof(out));
}

void tarjan(int u)
{
    low[u]=dfn[u]=++cnt;
    st[++top]=u; ins[u]=1;
    for(int i=head[u];i;i=E[i].nxt)
    {
        int 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])
    {
        colnum++;
        do{
            col[st[top]]=colnum;
            ins[st[top]]=0;
        }while(st[top--]!=u);
    }
}

int main()
{
    t=read();
    while(t--)
    {
        init();
        n=read();m=read(); 
        for(int i=1;i<=m;++i)
        {
            int u=read(),v=read();
            add(u,v);
        }
        for(int i=1;i<=n;++i)
        if(!dfn[i])tarjan(i);
        
        for(int u=1;u<=n;++u)
        for(int i=head[u];i;i=E[i].nxt)
        if(col[u]!=col[E[i].v])
        out[col[u]]=in[col[E[i].v]]=1;
        
        int ans1=0,ans2=0;
        for(int i=1;i<=colnum;++i)
        {
            if(!in[i])ans1++;
            if(!out[i])ans2++;
        }
        if(colnum==1)printf("0\n");
        else printf("%d\n",max(ans1,ans2));
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/niiick/article/details/82892227