POJ-2186 Popular Cows (SCC 强连通分量)

题目链接: http://poj.org/problem?id=2186

题目

Popular Cows
Time Limit: 2000MS
Memory Limit: 65536K
Total Submissions: 35430

Accepted: 14442
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

题意:

A认为B更受欢迎,B认为C更受欢迎,那么A认为C更受欢迎,求那头牛被所有牛认为更受欢迎。

题解:

为所有牛认为受欢迎,那么它的出度为0,但是,有环的话,环内所有牛都为所有牛认为受欢迎,所以先去环,然后看哪个环的出度为0,那么这个环的所有牛都被认为受欢迎。如果有两个环的出度都为0,很显然这两个环内的牛不可能互相认为受欢迎,那么答案为0。也就是求强连通分量,然后看是否有强连通分量的出度为0,如果有一个,那么这个强连通分量内的牛的数量就是答案。
判断强连通分量的出度的方法:枚举每一个点的每一条边,如果起始点和终止点都在一个强连通分量内,则该强连通分量的出度不增加,否则,增加一。

代码

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 10005;
const int M = 50005;
const int INF = 0x3f3f3f3f;
struct edge {
    int to, next;
}graph[M];
int totlen;
int head[N];
int n, m;
void init() {
    totlen = 0;
    memset(head, -1, sizeof head);
}
void addEdge(int u, int v) {
    graph[totlen] = {v, head[u]};
    head[u] = totlen++;
}
int Bcnt, Bindex, top;
// Bcnt 强连通分量的数量
// Bindex 当前搜索树的深度
int low[N]; // 栈内最小深度
int dfn[N]; // 当前点的深度
int belong[N];  // i点所属强连通的标号
int sta[N];
bool instack[N];
int tarjan(int u) {
    sta[top++] = u;
    instack[u] = true;

    low[u] = dfn[u] = ++Bindex;
    for(int i = head[u]; ~i; i = graph[i].next) {
        int v = graph[i].to;
        if(!dfn[v]) {
            tarjan(v);
            low[u] = min(low[u], low[v]);
        }else if(instack[v]){
            low[u] = min(low[u], dfn[v]);
        }
    }

    if(low[u] == dfn[u]) {
        Bcnt++;
        int v;
        do {
            v = sta[--top];
            instack[v] = false;
            belong[v] = Bcnt;  // 标记所属连通分量
        } while(v != u);
    }
}
// 求解强连通分量
void SCC() {
    Bcnt = Bindex = top = 0;
    memset(dfn, 0, sizeof dfn);
    for(int i = 1; i <= n; i++)
    if(!dfn[i])
        tarjan(i);
}
int main() {
    init();
    scanf("%d%d", &n, &m);
    for(int i = 0; i < m; i++) {
        int u, v;
        scanf("%d%d", &u, &v);
        addEdge(u, v);
    }
    SCC();
    int du[N];
    memset(du, 0, sizeof du);
    for(int u = 1; u <= n; u++) {
        for(int i = head[u]; i != -1; i = graph[i].next) {
            if(belong[graph[i].to] != belong[u]) {
                du[belong[u]]++;
            }
        }
    }

    int ansRt; // 记录答案所在的强连通分量
    int ans = 0;
    int zero = 0;
    for(int i = 1; i <= Bcnt; i++) {  // 统计强连通分量的出度
        if(du[i] == 0) {
            ansRt = i;
            zero++;
        }
    }

    if(zero == 1) {
        for(int i = 1; i <= n; i++) {
            if(belong[i] == ansRt)
                ans++;
        }
    }
    printf("%d\n", ans);

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值