HDU 2767 Proving Equivalences Tanjan+缩点 直接当缩点的模板吧

73 篇文章 21 订阅
12 篇文章 0 订阅

#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>

#include <iostream>
#include <cmath>
#include <algorithm>
#include <numeric>
#include <utility>

#include <cstring>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <string>
#include <memory.h>
using namespace std;

#define inf 0x3f3f3f3f
#define MAXN 50050
#define MAXM 100005
#define clr(x,k) memset((x),(k),sizeof(x))
#define cpy(x,k) memcpy((x),(k),sizeof(x))
#define Base 10000

#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
vector<int> vec[MAXN];
int n,m;
int id[MAXN],pre[MAXN],low[MAXN],s[MAXN],stop,cnt,scnt;
void init(){
    for(int i = 0;i <= n;i++) vec[i].clear();
    for(int i = 0,a,b;i < m;i++){
        scanf("%d %d",&a,&b);
        vec[a].push_back(b);
    }
    stop=cnt=scnt=0;
    clr(pre,-1);
    clr(id,0);
}
void Tarjan(int v,int n){
    int t,minc=low[v]=pre[v]=cnt++;
    vector<int>::iterator pv;
    s[stop++]=v;
    for(pv=vec[v].begin();pv!=vec[v].end();++pv){
        if(-1==pre[*pv]) Tarjan(*pv,n);
        if(low[*pv] < minc) minc=low[*pv];
    }
    if(minc<low[v]){
        low[v]=minc;return;
    }do{
        id[t=s[--stop]]=scnt;low[t]=n;
    }while(t!=v);
    ++scnt;
}
int in[MAXN],out[MAXN];
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%d %d",&n,&m);
        init();
        for(int i = 1;i <= n;i++)
            if(-1==pre[i])
                Tarjan(i,n);
        //缩点操作
        clr(in,0),clr(out,0);
        for(int u =  1;u <= n;u++){
            for(vector<int>::iterator v = vec[u].begin(); v != vec[u].end();v++){
                if(id[u]!=id[*v]){
                    out[id[u]]++;
                    in[id[*v]]++;
                }
            }
        }
        //计算
        int inNum,outNum;
        inNum = outNum=0;
        for(int i=0;i < scnt;i++){
            if(!in[i])
                inNum++;
            if(!out[i])
                outNum++;
        }
        if(scnt==1){
            printf("0\n");
        }else printf("%d\n",max(inNum,outNum));

    }
    return 0;
}

题目描述:

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?

  通过强连通分量Tarjan算法后,进行缩点,然后算强连通点的入度跟出度的分量数目即可。     答案即为入度为0或出度为0的点数目的最大值。  这点可以将缩点后图形堪称有向无环图,仿照树的定义,根为入度为0的点,叶子为出度为0的点。很明显的,要使其成为强连通图,令f为树根数,g为叶子数,则答案为max(f,g),特别的,当缩点只有一个点时,答案为0


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值