• 原题如下:

    Popular Cows
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 40746   Accepted: 16574

    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. 
  • 题解:建图显然,假设两头牛A和B都被其他所有牛认为是红人,那么显然A和B互相认为对方是红人,即存在一个包含A、B两个顶点的圈,或者说,A、B同属于一个强连通分量,反之,如果一头牛被其他所有牛认为是红人,那么其所属的强连通分量内的所有牛都被其他所有牛认为是红人。由此可知,把图进行强连通分量分解后,至多有一个强连通分量满足题目的条件。而进行强连通分解时,我们还可以得到各个强连通分量拓扑排序后的顺序,唯一可能成为解的只有拓扑序最后的强连通分量,,所以在最后,我们只要检查最后一个强连通分量是否从所有顶点可达就好了。该算法的复杂度为O(N+M)。
  • 代码:
     #include <cstdio>
    #include <stack>
    #include <vector>
    #include <algorithm>
    #include <cstring> using namespace std; stack<int> s;
    const int MAX_V=;
    bool instack[MAX_V];
    int dfn[MAX_V];
    int low[MAX_V];
    int ComponentNumber=;
    int index;
    vector<int> edge[MAX_V];
    vector<int> redge[MAX_V];
    vector<int> Component[MAX_V];
    int inComponent[MAX_V];
    int N, M;
    bool visited[MAX_V]; void add_edge(int x, int y)
    {
    edge[x].push_back(y);
    redge[y].push_back(x);
    } void tarjan(int i)
    {
    dfn[i]=low[i]=index++;
    instack[i]=true;
    s.push(i);
    int j;
    for (int e=; e<edge[i].size(); e++)
    {
    j=edge[i][e];
    if (dfn[j]==-)
    {
    tarjan(j);
    low[i]=min(low[i], low[j]);
    }
    else
    if (instack[j]) low[i]=min(low[i], dfn[j]);
    }
    if (dfn[i]==low[i])
    {
    ComponentNumber++;
    do
    {
    j=s.top();
    s.pop();
    instack[j]=false;
    Component[ComponentNumber].push_back(j);
    inComponent[j]=ComponentNumber;
    }
    while (j!=i);
    }
    } void rdfs(int v)
    {
    visited[v]=true;
    for (int i=; i<redge[v].size(); i++)
    {
    if (!visited[redge[v][i]])
    {
    rdfs(redge[v][i]);
    }
    }
    } int main()
    {
    memset(dfn, -, sizeof(dfn));
    scanf("%d %d", &N, &M);
    for (int i=; i<M; i++)
    {
    int x, y;
    scanf("%d %d", &x, &y);
    add_edge(x, y);
    }
    for (int i=; i<N+; i++)
    {
    if (dfn[i]==-) tarjan(i);
    }
    int v=Component[][];
    int num=Component[].size();
    rdfs(v);
    for (int i=; i<=N; i++)
    {
    if (!visited[i])
    {
    num=;
    break;
    }
    }
    printf("%d\n", num);
    }

Popular Cows(POJ 2186)的更多相关文章

  1. (连通图 缩点 强联通分支)Popular Cows -- poj --2186

    http://poj.org/problem?id=2186 Description Every cow's dream is to become the most popular cow in th ...

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

    Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= N <= 10, ...

  3. Popular Cows (POJ No.2186)

    Description Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= ...

  4. poj - 2186 Popular Cows && poj - 2553 The Bottom of a Graph (强连通)

    http://poj.org/problem?id=2186 给定n头牛,m个关系,每个关系a,b表示a认为b是受欢迎的,但是不代表b认为a是受欢迎的,关系之间还有传递性,假如a->b,b-&g ...

  5. Popular Cows(codevs 2186)

    题意: 有N(N<=10000)头牛,每头牛都想成为most poluler的牛,给出M(M<=50000)个关系,如(1,2)代表1欢迎2,关系可以传递,但是不可以相互,即1欢迎2不代表 ...

  6. poj 2186 Popular Cows (强连通分量+缩点)

    http://poj.org/problem?id=2186 Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissi ...

  7. POJ 2186 Popular Cows (强联通)

    id=2186">http://poj.org/problem? id=2186 Popular Cows Time Limit: 2000MS   Memory Limit: 655 ...

  8. poj 2186 Popular Cows 【强连通分量Tarjan算法 + 树问题】

    题目地址:http://poj.org/problem?id=2186 Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Sub ...

  9. 强连通分量分解 Kosaraju算法 (poj 2186 Popular Cows)

    poj 2186 Popular Cows 题意: 有N头牛, 给出M对关系, 如(1,2)代表1欢迎2, 关系是单向的且能够传递, 即1欢迎2不代表2欢迎1, 可是假设2也欢迎3那么1也欢迎3. 求 ...

随机推荐

  1. C#LeetCode刷题之#258-各位相加(Add Digits)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3860 访问. 给定一个非负整数 num,反复将各个位上的数字相加 ...

  2. LeetCode 861翻转矩阵后得分详细解法

    1. 题目内容 有一个二维矩阵 A 其中每个元素的值为 0 或 1 . 移动是指选择任一行或列,并转换该行或列中的每一个值:将所有 0 都更改为 1,将所有 1 都更改为 0. 在做出任意次数的移动后 ...

  3. 漏洞重温之XSS(中)

    漏洞重温之XSS(中) XSS挑战之旅 level8-level13 level8 第八关开局,发现button从搜索变成了友情链接,发现该页面情况跟前面不同,先右键查看代码,再进行尝试. 上测试代码 ...

  4. GRE 协议 - 和 ISP 用的协议不一样怎么办

    GRE 出现的背景: 随着网络(公司)规模的增大,越来越多的公司需要在跨区域之间建设自己的分公司.但随之也就出现了这样的问题,考虑这样一个场景.公司 A 在北京和上海间开设了两家公司,由于业务的需要, ...

  5. Java中的注解及自定义注解你用的怎么样,能不能像我这样应用自如?

    Java注解提供了关于代码的一些信息,但并不直接作用于它所注解的代码内容.在这个教程当中,我们将学习Java的注解,如何定制注解,注解的使用以及如何通过反射解析注解. Java1.5引入了注解,当前许 ...

  6. Golang Gtk+3教程:Grid布局

    在上个例子中我们使用了box布局,现在让我们来学习另一种布局--grid.其实这几种布局都大同小异,如果你看懂了上一个例子,想必使用grid也不是难事. 程序运行效果: package main im ...

  7. Python 函数为什么会默认返回 None?

    Python 有一项默认的做法,很多编程语言都没有--它的所有函数都会有一个返回值,不管你有没有写 return 语句. 本文出自"Python为什么"系列,在正式开始之前,我们就 ...

  8. 深度优先搜索(dfs)与出题感想

    在3月23号的广度优先搜索(bfs)博客里,我有提到写一篇深搜博客,今天来把这个坑填上. 第一部分:深度优先搜索(dfs) 以上来自百度百科. 简单来说,深度优先搜索算法就是——穷举法,即枚举所有情况 ...

  9. getAnnotation的一个坑

    // TableField annotation = f.getAnnotation(TableField.class); // 不建议使用这个,建议使用下面这个方法获取 TableField ann ...

  10. DataGrid添加进度条列

    DataGridColumn类型的继承树 DataGridColumn的派生类: 一般情况下DataGridBoundColumn和DataGridComboBoxColumn足以满足多数列的样式,如 ...