POJ 2186 Popular Cows【Tarjan缩点+建缩点图】

Popular Cows
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 36507 Accepted: 14876
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

题意:n头牛m个关系,(a,b)即牛a认为牛b受欢迎,若a认为b受欢迎,b认为c受欢迎,则a认为c受欢迎。问你在n头牛里有多少头牛被所有的牛认为是受欢迎的?
分析:
Tarjan预处理,把牛群给缩点,Tarjan的过程中保留每个scc的点集,最后找有且仅有一个的出度为0的点集,输出集合元素个数即可;
注意有可能最后出度为0的点集不只一个,如果点集大于1,那么就输出0;

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
typedef long long LL;

const int MAXN = 50010;
int stack[MAXN], col[MAXN], vis[MAXN];
int dfn[MAXN], low[MAXN], in[MAXN], out[MAXN];
int index, top, scc;
vector<int> V[MAXN], G[MAXN];

void init() {
    memset(dfn, 0, sizeof(dfn));
    memset(vis, 0, sizeof(vis));
    memset(out, 0, sizeof(out));
    memset(in, 0, sizeof(in));
    index = scc = top = 0;
    for(int i = 0; i < MAXN; i++) {
        V[i].clear();
        G[i].clear();
    }
}

inline void tarjan(int u) {
    int v;
    low[u] = dfn[u] = ++index;
    stack[top++] = u;
    vis[u] = 1;
    for(int i = 0; i < V[u].size(); ++i) {
        v = V[u][i];
        if(!dfn[v]) {
            tarjan(v);
            low[u] = min(low[v], low[u]);
        }
        else if(vis[v] && low[u] > dfn[v]) {
            low[u] = dfn[v];
        }
    }
    if(low[u] == dfn[u]) {
        scc++;
        do {
            v = stack[--top];
            vis[v] = 0;
            col[v] = scc; //染色,有利于后续建图
            G[scc].push_back(v); //保留每个scc的点集
        } while(v != u);
    }
}

void solve(int n) {
    for(int i = 1; i <= n; ++i) {
        if(!dfn[i]) {
            tarjan(i);
        }
    }
    memset(vis, 0, sizeof(vis));
    for(int i = 1; i <= n; ++i) {
        for(int j = 0; j < V[i].size(); ++j) {
            int v = V[i][j];
            if(col[v] == col[i]) continue;
            out[col[i]]++;
        }
    }
    int ans = 0, flag = 0;
    for(int i = 1; i <= scc; ++i) {
        if(!out[i]) {
            ans += G[i].size();
            flag++;
        }
    }
    if(flag > 1) printf("0\n"); //不受所有牛欢迎
    else printf("%d\n", ans);
}

int main() {
    int n, m, A, B;
    while(scanf("%d %d", &n, &m) != EOF) {
        init();
        while(m--) {
            scanf("%d %d", &A, &B);
            V[A].push_back(B);
        }
        solve(n);
    }
    return 0;
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值