关闭

POJ2186Popular Cows

757人阅读 评论(0) 收藏 举报
分类:
Popular Cows
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 21149   Accepted: 8620

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. 

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
#include <stack>
#define DEBUG 10
using namespace std;
const int maxn = 10000+10;
const int maxm = 50000+10;

int gn, gm;
vector<int> G[maxn], G2[maxn];
stack<int> S;
int pre[maxn], sccno[maxn], lowlink[maxn];
int dfs_clock, scc_cnt;
int weight[maxn];// the number of every scc.
//Accepted	2756K	782MS	G++	2296B	2013-12-01 22:54:30

void dfs(int u) {
    pre[u] = lowlink[u] = ++dfs_clock;
    S.push(u);
    for(int i = 0; i < (int)G[u].size(); i++) {
        int v = G[u][i];
        if(!pre[v]) {
            dfs(v);
            lowlink[u] = min(lowlink[u], lowlink[v]);
        } else if(!sccno[v]) {
            lowlink[u] = min(lowlink[u], pre[v]);
        }
    }
    if(lowlink[u] == pre[u]) {
        scc_cnt++;
        for(;;) {
            int x = S.top(); S.pop();
            sccno[x] = scc_cnt;
            if(x == u) break;
        }
    }
}


void find_scc(int n) {
    dfs_clock = scc_cnt = 0;
    memset(pre, 0, sizeof(pre));
    memset(sccno, 0, sizeof(pre));
    for(int i = 1; i <= gn; i++) {
        if(!pre[i]) {
            dfs(i);
        }
    }
}

void build_map() {
    int u, v;
    memset(weight, 0, sizeof(weight));
    for(int i = 1; i <= gn; i++) {
        int v = sccno[i];
        weight[v] += 1;
    }
#ifndef DEBUG
    for(int i = 1; i <= scc_cnt; i++) {
        printf("weight[%d] = %d\n", i, weight[i]);
    }
#endif
    for(int i = 1; i <= gn; i++) {
        for(int j = 0; j < (int)G[i].size(); j++) {
            u = sccno[i]; v = sccno[G[i][j]];
            if(u != v) {
                G2[u].push_back(v);
            }
        }
    }
}

int work() {
    vector<int> V;
    V.clear();
    for(int i = 1; i <= scc_cnt; i++) {
        if(!G2[i].size()) {
            V.push_back(i);
        }
    }
    if(V.size() > 1) return 0;
    else return weight[V[0]];
}

int main()
{
   // freopen("in", "r", stdin);
    int u, v;
    while(scanf("%d%d", &gn, &gm) != EOF) {
        for(int i = 1; i <= gm; i++) {
            scanf("%d%d", &u, &v);
            G[u].push_back(v);
        }
        find_scc(gn);
        build_map();
        int output = work();
        printf("%d\n", output);
    }
    return 0;
}

0
0

猜你在找
【直播】机器学习&数据挖掘7周实训--韦玮
【套餐】系统集成项目管理工程师顺利通关--徐朋
【直播】3小时掌握Docker最佳实战-徐西宁
【套餐】机器学习系列套餐(算法+实战)--唐宇迪
【直播】计算机视觉原理及实战--屈教授
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之矩阵--黄博士
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之凸优化--马博士
【套餐】Javascript 设计模式实战--曾亮
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:384171次
    • 积分:8364
    • 等级:
    • 排名:第2328名
    • 原创:434篇
    • 转载:80篇
    • 译文:13篇
    • 评论:71条
    记录成长的足迹
    编程是件有趣的事情,因为热爱所以专一!
    最新评论