poj 2186 强连通分量

本文介绍了一种解决牛群中流行度计算的问题算法。在一群牛中,通过指定的成对关系判断每只牛是否被其他所有牛认为是流行的。使用深度优先搜索和强连通分量算法来确定被视为最流行的牛的数量。
Popular Cows
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 29833 Accepted: 12102

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



代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <stack>
#include <cstring>
#include <vector>
using namespace std;
const int maxn = 10000+10;
struct node{
    int t, next;
}edge[50000+10];
int low[maxn];
int belg[maxn];
int st[maxn], sa[maxn];
int stk=0, sak=0;
int head[50000+10];
int cnt=0;
void addedge(int u, int v){
    edge[cnt].t = v;
    edge[cnt].next = head[u];
    head[u] = cnt;
    cnt++;
}

void Garbow(int now, int time, int &scc_num){
    st[++stk] = now;
    sa[++sak] = now;
    low[now] = ++time;
    for (int i=head[now]; i!=-1; i=edge[i].next){
        if (low[edge[i].t] == 0)
            Garbow(edge[i].t, time, scc_num);
        else if (belg[edge[i].t] == 0)
            while (low[sa[sak]] > low[edge[i].t])
            --sak;
    }
    if (sa[sak] == now){
    sak--;
    scc_num++;
    do{
        belg[st[stk]] = scc_num;
    }while(st[stk--] != now);
    }
}

int main() {
    int n, m;
    while (scanf("%d%d", &n, &m)!=EOF) {
        cnt = 0;
        sak = stk = 0;
        memset(belg, 0, sizeof(belg));
        memset(head, -1, sizeof(head));
        memset(low, 0, sizeof(low));
        int deg[maxn];
        vector<int>a;
        memset(deg, 0, sizeof(deg));
        for (int i=0; i<m; i++){
            int u, v;
            scanf("%d%d", &u, &v);
            addedge(u, v);
        }
        int scc_num=0, time=0;      
        for (int i=1; i<=n; i++)
            if (low[i] == 0)
                Garbow(i, time, scc_num);
	    for (int i=1; i<=n; i++)
            for (int j=head[i]; j!=-1; j=edge[j].next)
                if (belg[i] != belg[edge[j].t])       //就像进行将连通分量缩点。
                    deg[belg[i]]++;
        int ans = 0, pos;
        for (int i=1; i<=scc_num; i++)
            if (!deg[i])
                ans++, pos=i;
        if (ans == 1) {
            ans = 0;
            for (int i=1;i<=n; i++)
		    if (belg[i] == pos)
			    ans++;
            printf("%d\n", ans);
        }
        else
            puts("0");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值