POJ 2186 Popular Cows 受欢迎的奶牛

Popular Cows

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 45992 Accepted: 18779
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

USACO 2003 Fall

题目大意:
给出一些奶牛之间受欢迎的关系,(关系具有传递性,A欢迎B,B欢迎C,那么A也欢迎C)问有几头奶牛是受所有奶牛欢迎的
题目分析:
缩点求强连通分量,然后统计出度,最多只能有一个出度为0的点

// 这么多年不写Tarjan 居然是1A的  好哇塞啊
// 本来刚度这个题的时候 我以为是无向边 那我一想 Tarjan只要所有的点都在一起答案就是n  只要不是都在一起就是0呗
// 想了想才知道 是有向边
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
using namespace std;
#define Maxn 10005
struct Edge {
    int to,next;
}e[Maxn << 3];
int dfn[Maxn],low[Maxn],head[Maxn],tot,visx,sum[Maxn],cnt,tar[Maxn],out[Maxn];
bool exist[Maxn];
inline void Add_Edge(int u,int v) {
    e[++tot].to = v; e[tot].next = head[u]; head[u] = tot;
}
stack<int> st;
void Tarjan(int u) {
    low[u] = dfn[u] = ++visx;
    st.push(u); exist[u] = 1;
    for(int i=head[u]; i; i=e[i].next) {
        int v = e[i].to;
        if(!dfn[v]) {
            Tarjan(v);
            low[u] = min(low[u],low[v]);
        }
        else low[u] = min(low[u],dfn[v]);
    }
    if(low[u] == dfn[u]) {
        cnt++;// 缩点加一
        while(st.top() != u ) {
            tar[st.top()] = cnt;
            sum[cnt]++;
            st.pop();
        }
        tar[u] = cnt;
        sum[cnt]++;
        st.pop();// 弹出u
    }
    return ;
}

int main() {
    int n,m; scanf("%d %d",&n,&m);
    for(int u,v,i=1; i<=m; i++) {
        scanf("%d %d",&u,&v);
        Add_Edge(u,v);
    }
    memset(dfn,0,sizeof(dfn));
    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; j=e[j].next) {
            int v = e[j].to;
            if(tar[i] != tar[v]) out[tar[i]]++;// u,v 缩点之后不再同一个强连通分量 并且存在从u指向v的边
        }
    }
    int Ans = 0;
    for(int i=1; i<=cnt; i++)
        if(out[i] == 0) {
            if(Ans != 0) { printf("0\n"); return 0; }
            Ans = sum[i];
        }
    printf("%d\n",Ans);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

七情六欲·

学生党不容易~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值