• 原题如下:

    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. sql 语句(精品)

    GROUP BY: select avg(latency),projectName,data_trunc('hour'm\,_time_) as hour group by projectName,h ...

  2. Manacher(马拉车)算法(jekyll迁移)

    layout: post title: Manacher(马拉车)算法 date: 2019-09-07 author: xiepl1997 cover: 'assets/img/manacher.p ...

  3. 清晰详细、可测量、可达到、目标导向、有时间限定,SMART目标定制原则

    设定目标的时候需要考虑的问题,可以对已经设定的目标进行完善 S 目标是清晰的,明确的 M 目标可以衡量的,可以用来评估的 A 目标是可以达到的,但是达到的过程有难度 R 目标导向,设定的目标对大目标具 ...

  4. 双系统Linux和win10系统时间不一样。

    https://jingyan.baidu.com/article/456c463b4e6a5a0a5831443a.html

  5. 【HNOI2010】弹飞绵羊 - LCT

    题目描述 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置,每个装置设定初始弹力系 ...

  6. vue watch/ computed的应用(做一个简单的父子之间的传递/电话号码的搜索)

    父组件中当点击搜索的时候请求接口,然后把新的数据用 computed 传递给子组件 <van-search v-model="onSeachPhone" show-actio ...

  7. python去除 数据的 重复行

    原文链接:https://www.cnblogs.com/loren880898/p/11303672.html

  8. 随机陷阱和P值

    如果让大家写一个50次的抛硬币实验的可能结果(头像H或字T),多半人在连续三个一样的后,会换一下.因为大家都知道,连续一样的越多,概率越小,越不可能发生.大部分人不会去想,其实HHHTT和HHHHH发 ...

  9. kolla搭建ironic裸机服务

    参考链接: https://www.lijiawang.org/posts/kolla-ironic.html 准备ageng镜像: [root@control01 ~]# pip install d ...

  10. Java多线程_Atomic

    1.什么是Atomic?Atomic,中文意思是“原子的”,在java多线程中,有这样的一个包: java.util.concurrent.atomic——线程安全的原子操作包 这是JDK1.5的版本 ...