大一暑假第三周下强连通分量 H - Popular Cows

H - Popular Cows

有向图的强连通分量
题目链接

题目:
Every cow’s dream is to become the most popular cow in the herd. In a herd of N (1 <= N <= 10,000) cows, you are given up to M (1 <= M <= 50,000) ordered pairs of the form (A, B) that tell you that cow A thinks that cow B is popular. Since popularity is transitive, if A thinks B is popular and B thinks C is popular, then A will also think that C is
popular, even if this is not explicitly specified by an ordered pair in the input. Your task is to compute the number of cows that are considered popular by every other cow.

输入:

  • Line 1: Two space-separated integers, N and M

  • Lines 2…1+M: Two space-separated numbers A and B, meaning that A thinks B is popular.

输出:

  • Line 1: A single integer that is the number of cows who are considered popular by every other cow.

样例:
输入:
3 3
1 2
2 1
2 3
输出:
1
————————————————————————————————————————
题意:
给你n个点,m条单向的线,请你求出有几个点可以被其他所有点到达。请输出这种点的个数。

思路:
先遍历Tarjan算法,把所有的强连通分量找出来;显然,在同一个强连通分量中的所有点都是可以互相到达的,但问题是不同强连通分量中的点不一定能相互到达,所以这就是本题的重点所在;接下来就把图反向建立,如果一个点可以到达任何一个点,就说明这个点可以被其他所有点到达。最后统计个数就行了。
————————————————————————————————————————
代码:

#include<stdio.h>
#include<math.h>
#include<iostream>
#include<vector>
#include<stack>
#include<string.h>
#include<algorithm>
#define inf 99999999
using namespace std;
const int maxx=100010;
int n,m;
int cnt,k;
int book[maxx],low[maxx],dfn[maxx];//Tarjan
int first[maxx],ifirst[maxx];//建立邻接表
int v[maxx],next[maxx],iv[maxx],inext[maxx];
stack<int>s;
void dfs(int a)//Tarjan
{
    int i,j,z;
    low[a]=dfn[a]=++k;
    s.push(a);
    for(i=first[a];i!=-1;i=next[i])
    {
        if(dfn[v[i]]==0)
        {
            dfs(v[i]);
            low[a]=min(low[a],low[v[i]]);
        }
        else if(book[v[i]]==0)
        {
            low[a]=min(low[a],dfn[v[i]]);
        }
    }
    if(low[a]==dfn[a])
    {
        cnt++;
        while(!s.empty())
        {
            z=s.top();
            s.pop();
            book[z]=cnt;//是否进入连通图并且记录是第几个进入连通图的点
            if(z==a)
                break;
        }
    }
}
void ddfs(int a)
{
    int i,j,z;
    book[a]=1;
    for(i=ifirst[a];i!=-1;i=inext[i])
    {
        if(book[iv[i]]==0)
            ddfs(iv[i]);
    }
}
int main()
{
    int i,j,z,a,b,t,tt,sum;
    cin>>n>>m;
    memset(first,-1,sizeof(first));
    memset(ifirst,-1,sizeof(ifirst));
    t=tt=0;
    for(i=1;i<=m;i++)
    {
        cin>>a>>b;
        v[t]=b;
        next[t]=first[a];
        first[a]=t;
        t++;

        iv[tt]=a;
        inext[tt]=ifirst[b];
        ifirst[b]=tt;
        tt++;
    }
    k=cnt=0;
    memset(low,0,sizeof(low));
    memset(dfn,0,sizeof(dfn));
    memset(book,0,sizeof(book));
    for(i=1;i<=n;i++)
    {
        if(dfn[i]==0)
            dfs(i);
    }
    sum=0;
    int u;
    for(i=1;i<=n;i++)
    {
        if(book[i]==1)//因为该标点是在回溯中执行的,所以标号为1的点就是最后一个点
        {
            sum++;
            u=i;
        }
    }
    int r=0;
    memset(book,0,sizeof(book));
    ddfs(u);//反向遍历
    for(i=1;i<=n;i++)
    {
        if(book[i])
            r++;
    }
    if(r==n)
        cout<<sum<<endl;
    else
        cout<<"0"<<endl;
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值