HDU2767-Proving Equivalences(强连通+缩点+DGA性质)--Tarjan模板

探讨如何确定证明一组数学语句等价所需的最少额外证明步骤,通过构造图论模型,利用Tarjan算法简化问题,并给出具体实现代码。

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
    题目:HDU2767
    题意:给你一些语句编号1~n,其中有些语句Xi能够证明Xj,问(最少)还需要证明几条语句之间的关系,使得所有语句之间的关系成为一个环。
    思路:题意说的很明确了,问最少加几条边能够使一个DGA(有向无环图)成为一个环,但是题目所给的图中可能含有环,所以我们先用Tarjan算法缩点,得到一个DGA,然后利用DGA的性质:将一个DGA变成一个环,最少需要加的边数=max(出度为0的点的个数,入度为0的点的个数),即可得到答案。
    AC代码:

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
#include<queue>
#define met(s,k) memset(s,k,sizeof s)
#define scan(a) scanf("%d",&a)
#define scanl(a) scanf("%lld",&a)
#define scann(a,b) scanf("%d%d",&a,&b)
#define scannl(a,b) scanf("%lld%lld",&a,&b)
#define scannn(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define prin(a) printf("%d\n",a)
#define prinl(a) printf("%lld\n",a)
using namespace std;
typedef long long ll;
const int maxn=20005;
const double eps=1e-4;
int dfn[maxn],dep[maxn],c[maxn],r[maxn],color[maxn],p[maxn],num,stacks[maxn],vis[maxn],cont,lid,k,n,m;
struct edge
{
    int en,next;
}e[3*maxn];
void init()
{
    met(p,-1);
    num=0;
    k=0;
    cont=1;
    lid=-1;
    met(c,0);
    met(r,0);
    met(vis,0);
    met(dfn,0);
    met(dep,0);
    met(color,0);
}
void add(int st,int en)
{
    e[num].en=en;
    e[num].next=p[st];
    p[st]=num++;
}
void tarjan(int u)
{
    vis[u]=1;
    dfn[u]=dep[u]=cont++;
    stacks[++lid]=u;
    for(int i=p[u];i!=-1;i=e[i].next)
    {
        int v=e[i].en;
        if(vis[v]==0)tarjan(v);
        if(vis[v]==1)dep[u]=min(dep[u],dep[v]);
    }
    if(dfn[u]==dep[u])
    {
        k++;
        do
        {
            color[stacks[lid]]=k;
            vis[stacks[lid]]=-1;
        }
        while(stacks[lid--]!=u);
    }
}
int main()
{
    int t;
    scan(t);
    while(t--)
    {
        scann(n,m);
        init();
        for(int i=0;i<m;i++)
        {
            int x,y;
            scann(x,y);
            add(x,y);
        }
        for(int i=1;i<=n;i++)
        {
            if(vis[i]==0)tarjan(i);
        }
        if(k==1)
        {
            prin(0);
            continue;
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=p[i];j!=-1;j=e[j].next)
            {
                  int v=e[j].en;
                  if(color[i]!=color[v])
                  {
                      c[color[i]]++;
                      r[color[v]]++;
                  }
            }
        }
        int chu=0,ru=0;
        for(int i=1;i<=k;i++)
        {
            if(c[i]==0)chu++;
            if(r[i]==0)ru++;
        }
        prin(max(chu,ru));
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值