其他分享
首页 > 其他分享> > HDU 2767(tarjin)

HDU 2767(tarjin)

作者:互联网

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 ©, 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:

Output
Per testcase:

Sample Input

2
4 0
3 2
1 2
1 3

Sample Output

4
2

题意:这个题其实相当于数学中的命题,给我们n个假设,和m个命题的假设和结论,然后问我们,还需要多少个命题才可以让知道一个假设,可以推出所有的结论。

解题思路:我们按照假设和结论的关系,由假设可以推出结论来建立有向边,然后如果他们形成了一个环,就表示是可以互相证明的,即满足题意,那么我们要求的就是需要加多少条边才能让所有点都形成一个环。我们将所有的环都缩成一个点,然后再建立图,如果要让所有的点都形成一个环那么就是消灭所有的入度为0的点和出度为0的点,我们将出度为0的点和入度为0的点交叉相连(即连成一条链),那么其实答案就是 max(入度为0的点的个数,出度为0的点的个数)。当然,这里要特判一下开始的时候就是一个环的情况,此时要输出0。
代码:

#pragma GCC optimize(2)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <bitset>
#include <queue>
//#include <random>
#include <time.h>
using namespace std;
#define int long long
#define ull unsigned long long
#define ls root<<1
#define rs root<<1|1
const int maxm = 50000+7;
const int maxn=20000+7;
//std::mt19937 rnd(time(NULL));
struct edge
{
    int v,next;
}e[maxm];
int head[maxn],cnt,dfn[maxn],low[maxn],tot,st[maxn],top,num[maxn],col,vis[maxn],in[maxn],out[maxn];
inline void add(int a,int b)
{
    e[++cnt]=edge{b,head[a]};
    head[a]=cnt;
}
void tarjin(int u)
{
    dfn[u]=++tot;low[u]=tot;
    st[++top]=u;
    for(int i=head[u];i;i=e[i].next){
        int v=e[i].v;
        if(!dfn[v]){
            tarjin(v);
            low[u]=min(low[v],low[u]);
        }
        else if(!vis[v]){
            low[u]=min(low[u],dfn[v]);
        }
    }
    if(dfn[u]==low[u]){
        num[u]=++col;
        vis[u]=1;
        while(st[top]!=u){
            int x=st[top];
            num[x]=col,vis[x]=1;
            top--;
        }
        top--;
    }
}
signed main()
{
    int t;
    scanf("%lld",&t);
    while(t--){
        int n,m;
        scanf("%lld%lld",&n,&m);
        memset(head,0,sizeof head);
        cnt=tot=col=top=0;
        memset(dfn,0,sizeof dfn);
        memset(low,0,sizeof low);
        memset(vis,0,sizeof vis);
        memset(num,0,sizeof num);
        memset(in,0,sizeof in);
        memset(out,0,sizeof out);
        for(int i=1;i<=m;i++){
            int a,b;
            scanf("%lld%lld",&a,&b);
            add(a,b);
        }
        for(int i=1;i<=n;i++){
            if(!dfn[i]) tarjin(i);
        }
        for(int i=1;i<=n;i++){
            for(int j=head[i];j;j=e[j].next){
                int v=e[j].v;
                if(num[v]!=num[i])
                in[num[v]]++,out[num[i]]++;
            }
        }
        if(col==1){
            puts("0");
            continue;
        }
        int ans1=0,ans2=0;
        for(int i=1;i<=col;i++){
            if(!in[i])ans1++;
            if(!out[i])ans2++;
        }
        printf("%lld\n",max(ans1,ans2));
    }
}

坑边闲话:之前做过类似的题,大家有兴趣可以去看一下POJ 1236,这个题和本题类似,多求了一个东西,本来之前做了那道题,然后这次做这道题的时候还是忘了特判,导致WA了一发。

东西和南北 发布了54 篇原创文章 · 获赞 16 · 访问量 4999 私信 关注

标签:HDU,implies,2767,equivalent,implications,number,statements,tarjin,include
来源: https://blog.csdn.net/qq_44641782/article/details/104174635