POJ -2186 Popular Cows(Tarjan,缩点)

Popular Cows

Time Limit: 2000MSMemory Limit: 65536K

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

强连通真有(che)趣(dan)!
相较于普通的强连通多了一个缩点(what?),虽然不理解,看着模板还是写了出来,程序尽力标记了。
参考链接:
https://www.cnblogs.com/xiaoningmeng/p/6071568.html

#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <vector>
#include <stack>
using namespace std;

#define M 10010

int low[M];//最小的子节点,相当于一棵树最开始的那个点
int bfn[M];//记录这个点搜索的次序编号(时间戳)
int m, n;
int cnt, cat;//时间戳,集合的序号
int vit[M];//标记数组
//只是多了这三个数组,和与其相关的操作
int point_num[M];//某个数所在集合的编号**
vector<int> point_list[M];//某个集合所含的元素**
int out[M];//某个集合的出度**
vector<int>Map[M];//存路径
stack<int>S;

void tarjan(int x)
{
    low[x]=bfn[x]=++cnt;
    vit[x]=1;
    S.push(x);
    for(int i=0; i<Map[x].size(); i++)
    {
        int t=Map[x][i];
        if(!bfn[t])
        {
            tarjan(t);
            low[x]=min(low[x], low[t]);
        }
        else if(vit[t])
            low[x]=min(low[x], bfn[t]);
    }
    if(low[x]==bfn[x])
    {
        cat++;
        while(!S.empty())
        {
            int t=S.top();
            S.pop();
            vit[t]=0;
            point_list[cat].push_back(t);//将元素t加入编号为cat的集合中**
            point_num[t]=cat;//元素t所在集合编号为cat**
            if(x==t)
                break;
        }
    }
}

void solve()
{
    memset(low, 0, sizeof(low));
    memset(bfn, 0, sizeof(bfn));
    memset(vit, 0, sizeof(vit));
    cnt=cat=0;
    for(int i=1; i<=n; i++)
        if(!bfn[i])
            tarjan(i);
}

int main()
{
    while(scanf("%d%d", &n, &m)!=EOF)
    {
        while(!S.empty())
            S.pop();
        for(int i=1; i<=n; i++)
            Map[i].clear();

        for(int i=0; i<m; i++)
        {
            int a, b;
            scanf("%d%d", &a, &b);
            Map[a].push_back(b);
        }
        solve();
        for(int i=1; i<=n; i++)//n个点遍历
        {
            for(int j=0; j<Map[i].size(); j++)//每个点连接的点
            {
                int t=Map[i][j];
                if(point_num[i]!=point_num[t])//若i点和与i相连的点在两个强连通分量中
                    out[point_num[i]]++;//i点所在集合的出度+1
            }
        }
        int zero_sum=0, pos;
        for(int i=1; i<=cat; i++)//查询出度为0的集合
            if(out[i]==0)
                pos=i, zero_sum++;

        if(zero_sum==1)//出度为零的集合的数量=1才会买礼物
            printf("%d\n", point_list[pos].size());
        else
            printf("0\n");
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值