HDU2767-Proving Equivalences(強連通+縮點+DGA性質)--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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章