洛谷 P2341 受欢迎的牛(强连通分量)

题目链接:

https://www.luogu.org/problemnew/show/P2341

题意:

给定一个有向图,如果 a a a能到 b b b b b b能到 c c c,那么 a a a也能到 c c c,问存在多少个这样的点,使得从所有点通过有向边都可以到达它,输出个数

分析:

首先考虑强连通量内任意两点可互相可达,那么如果强连通外的所有点都可以到达任意一个强连通内点,那么这个强连通分量的所有点都是属于结果的集合的,那么这样只要求出所有的强连通分量,缩点之后求,出度为0的强连通分量,然后输出这个强连通分量中点的个数;需要注意的是,如果存在多个出度为0的强连通分量,那么可以想象,假设有两个强连通分量,那么显然这两个强连通分量是不存在从任意一方抵达令一方的情况的,所以这时候结果集合点的个数为0; 比如存在孤立的集合或点的情况;

代码:
#include<iostream>
#include<string>
#include<queue>
#include<set>
#include<vector>
#include<stack>
#include<map>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;

const int inf=0x7f7f7f7f;
const int maxn=5e4+50;
const int N=1e4+50;
typedef long long ll;
typedef struct{
    int u,v,next,lca;
}Edge;
Edge e[2*maxn];

typedef struct B{
    int l,r;
    ll sum,lazy;
    void update(int value){
        sum+=(r-l+1)*value;
        lazy+=value;
    }
}Tree;
Tree tree[4*maxn];
int cnt,head[maxn];

void add(int u,int v){
    e[cnt].u=u;
    e[cnt].v=v;
    // e[cnt].w=w;
    // e[cnt].f=f;
    e[cnt].next=head[u];
    head[u]=cnt++;
    // e[cnt].u=v;
    // e[cnt].v=u;
    // e[cnt].w=0;
    // e[cnt].f=-f;
    // e[cnt].next=head[v];
    // head[v]=cnt++;
}

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

//_scc[u]存u点所在强连通分量的标号,out[x]存标号为x的强连通分量的出度,
//cot[x]存标号为x的连通量的点的数目
int sum,t,res,n,m,a,b,low[N],dfn[N],_time,vis[N],num,_scc[N],out[N],cot[N];
int stacK[N];


void tarjan(int u){
    dfn[u]=low[u]=++_time;
    vis[u]=1;
    stacK[num++]=u;
    for(int i=head[u];i!=-1;i=e[i].next){
        int v=e[i].v;
        if(!dfn[v]){
            tarjan(v);

            low[u]=min(low[v],low[u]);
        }
        else if(vis[v])low[u]=min(dfn[v],low[u]);
    }
    if(low[u]==dfn[u]){
        res++;
        do{
            t=stacK[--num];
            vis[t]=0;
            _scc[t]=res;
            cot[res]++;
        }while(t!=u);
    }
}

int main() {
    cin>>n>>m;
    memset(head,-1,sizeof(head));
    for(int i=0;i<m;i++){
        cin>>a>>b;
        add(a,b);
    }
    for(int i=1;i<=n;i++){
        if(!dfn[i])tarjan(i);
    }
    //cout<<res<<endl;
    for(int i=0;i<cnt;i++){
        int u=e[i].u,v=e[i].v;
        if(_scc[u]!=_scc[v]){
            out[_scc[u]]++;
            //cout<<_scc[u]<<" "<<u<<" "<<v<<endl;
        }
    }
    int outnum=0;
    for(int i=1;i<=res;i++){
        if(out[i]==0){
            outnum++;
            sum+=cot[i];
            if(outnum>=2){
                cout<<0<<endl;
                return 0;
            }
        }
    }
    cout<<sum<<endl;
    return 0;
}   

(仅供个人理解)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值