Poj2186Popular Cows

本文介绍了一种基于强连通分量算法解决特定问题的方法,通过构建图模型并运用Tarjan算法找到图中的强连通分量,最终确定哪些节点被视为最受欢迎的节点。
Popular Cows
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 31533 Accepted: 12817

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. 
 
【题目分析】
  题目大概是有好多牛,牛(们)之间具有某种关系(鬼知道),这种关系具有传递性,如果有 A欢迎B和B欢迎C,那么就有A欢迎C。我们要找到能让其他所有的牛都欢迎的牛。
  (废话真多)
  就是求强连通分量了,然后找到一个强连通分量,缩点,最后找出度为0的点(这里可以自己草纸划一下),如果有多个出度为0的点,那么肯定没有牛被所有牛欢迎,如果只有一个,输出这个点所代表的强连通分量的长度!!!md
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
const int maxn=12000;
vector<int>tu[maxn];
vector<int>lt[maxn];
int n,m,lts=0;
int js=0;
int dfn[maxn],low[maxn];
int zhan[maxn],top=0;
bool isins[maxn];
int num[maxn];//num[i]表示i点所在的强连通分量的编号 
int d[maxn];
void tarjan(int i)//hhhh 
{
    int j;
    dfn[i]=low[i]=++js;
    isins[i]=1;
    zhan[top++]=i;
    for(int j=0;j<tu[i].size();j++)
    {
        int tp=tu[i][j];
        if(dfn[tp]==-1)
            tarjan(tp),
            low[i]=min(low[i],low[tp]);
        else if(isins[tp])
            low[i]=min(low[i],dfn[tp]);
    }
    if(dfn[i]==low[i])
    {
        lts++;
        do{
            j=zhan[--top];
            isins[j]=0;
            lt[lts].push_back(j);
            num[j]=lts;//j点所在的强连通分量的编号为lts 
        }while(i!=j); 
    }
}
void solve(int n)
{
    memset(dfn,-1,sizeof dfn);
    memset(low,-1,sizeof low);
    memset(zhan,-1,sizeof zhan);
    memset(isins,0,sizeof isins);
    for(int i=0;i<n;i++)
        if(dfn[i]==-1)
            tarjan(i);
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        tu[x-1].push_back(y-1);//我从0开始啊 
    }
    solve(n);
    for(int i=0;i<n;i++)
        for(int j=0;j<tu[i].size();j++)
        {
            int t=tu[i][j];
            if(num[i]!=num[t])//如果i点与他指向的点不在同一个强连通分量中 
                d[num[i]]++;//i点所在的强连通分量的出度+1 
        }
    int pos=-1;
    int cnt=0;
    for(int i=1;i<=lts;i++)
        if(d[i]==0)//找到出度为0的强连通分量 
            cnt++,
            pos=i;
    if(cnt==1) cout<<lt[pos].size();//如果只有一个出度为0的强连通分量,那么这个强连通分量的长度即答案 
    else cout<<"0";//woc
    return 0;
}

 

转载于:https://www.cnblogs.com/xiaoningmeng/p/6071568.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值