poj2816

Popular Cows
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 29799 Accepted: 12090

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模板
#include<cstdio>
#include<cstring>
#include<iostream>
#include<vector>
#include<stack>
using namespace std;
#define N 10010
vector<int>grap[N];//稀疏图,用邻接表表示图
stack<int>s;//
int low[N];//low[u] 为u或u的子树能够追溯到的最早的栈中节点的次序编号  
int dfn[N];//dfn[u] 为u搜索的次序编号(时间戳)  
int mark[N];//标记是否在栈中  
int id[N];//id[i] = j 表示原图的点i缩点后为点j  
int pd;//顶点的前序编号
int sd;//记录总共将图缩成多少个点 
int sum[N];//记录sd编号的有几个点缩成 
void tarjan(int v){
    low[v]=dfn[v]=++pd;
    s.push(v); 
    mark[v]=1;
    for(int i=0;i<grap[v].size();i++){
        int w=grap[v][i];
        if(!dfn[w]){
            tarjan(w);
            low[v]=min(low[v],low[w]);//v或v的子树能够追溯到的最早的栈中节点的次序编号 
        }
        else if(mark[w]){//(v,w)为后向边
            low[v]=min(low[v],dfn[w]);
        }    
    }
    int u;
    if(low[v]==dfn[v]){//满足强连通分支条件,进行缩点 
        sd++;
        do{
            u=s.top();
            s.pop();
            id[u]=sd;//缩点
            sum[sd]++;
            mark[u]=0;//出栈解除标记

        }while(u!=v);
    }
}
int main(){
    int n,m,a,b;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++){
        scanf("%d%d",&a,&b);
        grap[a].push_back(b);
    }
    for(int i=1;i<=n;i++){
        if(!dfn[i]) tarjan(i);
    }
    //if(sd==1) {printf("0\n");return 0;}//如果图已经为强连通图,over 
    int in[N]={0},out[N]={0};
    for(int i=1;i<=n;i++){//求缩点后,各个顶点的出度和入度 
        for(int j=0;j<grap[i].size();j++){
            int k=grap[i][j];
            if(id[i]!=id[k]){
                in[id[k]]++;
                out[id[i]]++;
            }
        }
    }
    int ans=0,p=0;
    for(int i=1;i<=sd;i++){
        if(!out[i]){
            ans++;p=i;
        }
    }
    printf("%d\n",ans==1?sum[p]:0);
    return 0;
}

 

转载于:https://www.cnblogs.com/shenben/p/5633373.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值