• 原题如下:

    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);
    }

最新文章

  1. grunt快速入门
  2. VBA学习思路
  3. Javascript优化细节:短路表达式
  4. Filter体现职责链模式
  5. php使用正则过滤js脚本代码实例
  6. yii 自定义组件的调用
  7. [设计模式] javascript 之 适配器模式
  8. css的伪元素
  9. Hibernate(二)——POJO对象的操作
  10. 实战nginx 基础知识总结(一)1.1
  11. RxJava操作符(09-算术/聚合操作&amp;连接操作)
  12. python模块之sys与os
  13. nginx防止DDOS攻击配置
  14. Linux MySql 安装与配置
  15. hibernate懒加载
  16. (转)Xen Server删除Local Storage
  17. word如何让单页变横向
  18. 团队作业——王者光耀:team
  19. Elasticsearch前沿:ES 5.x改进详解与ES6展望
  20. 【WIN7】windows\system32 下的几乎所有文件的简单说明【1】

热门文章

  1. 利用BeautifulSoup去除HTML指定标签和去除注释
  2. 在Linux使用虚拟环境
  3. 下面POM插件的作用是转换包名,修改tomcat跳转端口
  4. 【NOI2014】动物园 - KMP
  5. PYTHON替代MATLAB在线性代数学习中的应用(使用Python辅助MIT 18.06 Linear Algebra学习)
  6. 笔记:Xshell、更新源、安装软件
  7. java泛型笔记
  8. 正则表达式截取xml
  9. 清空ARP缓存
  10. 仿京东BOE官网 html代码