Popular Cows

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 40304 Accepted: 16412

Description

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. 

Input

* 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. 

Output

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

Sample Input

3 3
1 2
2 1
2 3

Sample Output

1

Hint

Cow 3 is the only cow of high popularity. 

Source

 
Tarjan强连通分量+缩点。
首先我们跑一遍tarjan,然后把每个强连通分量缩成一个点。
再看出度为0的点是否有一个,再看这个点里有多少未缩点以前的点。
 
如何实现?
我们用一ind[i]表示i的点缩点后在哪个集合里。
再统计出度,遍历u的边v,如果不在一个集合,ind[u]入度加1.
遍历所有集合,如果多个集合入度为0,输出0
否则输出这个集合的个数。
看有多少个点ind[v]=x就好了。
#include <stack>
#include <cstdio>
#include <cstring>
#define  maxn 100005
using namespace std;
stack<int> s;
int dfn[maxn],low[maxn],head[maxn],f[maxn];
bool vis[maxn];
int ind[maxn];
int tot,cnt,sum;
struct edges
{
    int u,v,next;
}edge[maxn];
int n,m;
void addedge(int u,int v)
{
    edge[cnt].u=u;
    edge[cnt].v=v;
    edge[cnt].next=head[u];
    head[u]=cnt++;
}
void Tarjan(int num)
{
    dfn[num]=low[num]=++tot;
    vis[num]=true;
    s.push(num);
    for(int i=head[num];i!=-1;i=edge[i].next)
    {
        int v=edge[i].v;
        if(!dfn[v])
        {
            Tarjan(v);
            low[num]=min(low[num],low[v]);
        }
        else if(vis[num])
        {
            low[num]=min(low[num],dfn[v]);
        }
    }
    if(dfn[num]==low[num])
    {
        sum++;
        while(true)
        {
            int now=s.top();
            s.pop();
            vis[now]=false;
            ind[now]=sum;
            if(now==num) break;
        }
    }
}
int main()
{
    freopen("in.txt","r",stdin);
    while(~scanf("%d%d",&n,&m))
    {
        memset(head, -1, sizeof(head));
        memset(dfn,0, sizeof(dfn));
        memset(low,0, sizeof(low));
        memset(f,0, sizeof(f));
        memset(vis,false, sizeof(vis));
        memset(ind,0, sizeof(ind));
        tot=sum=cnt=0;
        while(!s.empty()) s.pop();
        for (int i = 1; i <= m; i++)
        {
            int u, v;
            scanf("%d%d", &u, &v);
            addedge(u, v);
        }
        for (int i = 1; i <= n; i++)
        {
            if (!dfn[i]) Tarjan(i);
        }
        for (int i = 1; i <= n; i++)
        {
            for (int j = head[i]; j != -1; j = edge[j].next)
            {
                int v = edge[j].v;
                if (ind[i] != ind[v])
                {
                    f[ind[i]]++;
                }
            }
        }
        int tol = 0;
        int index;
        for (int i = 1; i <= sum; i++)
        {
            if (!f[i])
            {
                tol++;
                index = i;
            }
        }
        int ans = 0;
        if (tol == 1)
        {
            for (int i = 1; i <= n; i++)
            {
                if (ind[i] == index) ans++;
            }
            printf("%d\n", ans);
        } else printf("0\n");
    }
    return 0;
}

  

转载于:https://www.cnblogs.com/zyf3855923/p/9602051.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值