POJ 2186 Popular Cows(Targin缩点)

时间:2022-09-21 19:24:45
Popular Cows
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 31808   Accepted: 12921

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,B羡慕C时,可以认为C也被A羡慕。问N头牛中,有几头牛被其他所有牛羡慕。

题解:根据样例可以看出,这个图不是DAG图,但是我们可以通过targin缩点,使之成为DAG图,对于DAG图,我们知道,如果一头牛有出度,那么它就不是被其他所有牛仰慕的牛,如果其出度为0那么其有可能成为被其他所有牛仰慕的牛,但是当出度为0的牛超过1时,便不存在被除自身外其他牛仰慕的牛,因为肯定有另外一头出度为0的牛不仰慕它。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 10005;
int tot, top, scc_cnt, index;
int head[maxn], dfn[maxn], low[maxn], outde[maxn], belong[maxn], st[maxn], inst[maxn], cnt[maxn];
struct Edge
{
    int v, next;
} edge[maxn*maxn];

void init()
{
    tot = top = index = scc_cnt = 0;
    memset(head, -1, sizeof(head));memset(belong, 0, sizeof(belong));
    memset(dfn, 0, sizeof(dfn));memset(low, 0, sizeof(low));
    memset(st, 0, sizeof(st));memset(inst, 0, sizeof(inst));
    memset(outde, 0, sizeof(outde)); memset(cnt, 0, sizeof(cnt));
}

void addedge(int u, int v)
{
    edge[tot] = (Edge)
    {
        v, head[u]
    };
    head[u] = tot++;
}

void targin(int u)
{
    int v;
    dfn[u] = low[u] = ++index;
    st[++top] = u;
    inst[u] = 1;
    for (int i = head[u];i != -1;i = edge[i].next)
    {
        v = edge[i].v;
        if (!dfn[v])
        {
            targin(v);
            low[u] = min(low[u],low[v]);
        }
        else if (inst[v])
            low[u] = min(low[u],dfn[v]);
    }
    if (dfn[u] == low[u])
    {
        scc_cnt++;
        do
        {
            v = st[top--];
            inst[v] = 0;
            belong[v] = scc_cnt;
            cnt[scc_cnt]++;
        }
        while (u != v);
    }
}

int main()
{
    int N, M, u, v, res, sum = 0;
    init();
    scanf("%d%d", &N, &M);
    for (int i = 0; i < M; i++)
    {
        scanf("%d%d", &u, &v);
        addedge(u, v);
    }
    for (int i = 1; i <= N; i++)  if (!dfn[i])    targin(i);
    for (int i = 1; i <= N; i++)
    {
        for (int j = head[i]; ~j; j = edge[j].next)
        {
            int v = edge[j].v;
            if (belong[i] != belong[v])
            {
                outde[belong[i]]++;
            }
        }
    }
    for (int i = 1; i <= scc_cnt; i++)
    {
        if (!outde[i])
        {
            res = i;
            sum++;
        }
    }
    if (sum > 1)    printf("0\n");
    else    printf("%d\n", cnt[res]);
}

  

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

  1. POJ 2186 Popular cows&lpar;SCC 缩点&rpar;

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

  2. POJ 2186 Popular Cows tarjan缩点算法

    题意:给出一个有向图代表牛和牛喜欢的关系,且喜欢关系具有传递性,求出能被所有牛喜欢的牛的总数(除了它自己以外的牛,或者它很自恋). 思路:这个的难处在于这是一个有环的图,对此我们可以使用tarjan算 ...

  3. 强连通分量分解 Kosaraju算法 &lpar;poj 2186 Popular Cows&rpar;

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

  4. poj 2186 Popular Cows &lpar;强连通分量&plus;缩点&rpar;

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

  5. tarjan缩点练习 洛谷P3387 【模板】缩点&plus;poj 2186 Popular Cows

    缩点练习 洛谷 P3387 [模板]缩点 缩点 解题思路: 都说是模板了...先缩点把有环图转换成DAG 然后拓扑排序即可 #include <bits/stdc++.h> using n ...

  6. POJ 2186 Popular Cows &lpar;强联通&rpar;

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

  7. poj 2186 Popular Cows 【强连通分量Tarjan算法 &plus; 树问题】

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

  8. poj 2186 Popular Cows【tarjan求scc个数&amp&semi;&amp&semi;缩点】【求一个图中可以到达其余所有任意点的点的个数】

    Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 27698   Accepted: 11148 De ...

  9. poj 2186 Popular Cows

    Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 29908   Accepted: 12131 De ...

随机推荐

  1. 自动拒绝恶意IP远程登录Linux服务器脚本

    当我们已经配置了iptables防火墙,我们允许22端口对外网所有人访问,当然这也是为了方便,我们在任何地方都连接上,没有做VPN,也没有做ssh密钥验证,但是我们的密码设置得非常复杂,大小写.特殊符 ...

  2. Javascript开发之工具归纳

    写在前面 由于JS开发对我来说是全新的技术栈,开发过程中遇到了各种各样的框架.工具,同时也感叹一下相对于.Net的框架(工具框架以及测试框架等)JS框架真的是太丰富了.社区的力量果然强大---也是由此 ...

  3. storm概念学习及流处理与批处理的区别

    在过去10 年中,随着互联网应用的高速发展,企业积累的数据量越来越大,越来越多.随着Google MapReduce.Hadoop 等相关技术的出现,处理大规模数据变得简单起来,但是这些数据处理技术都 ...

  4. Ext中获取button的思考

    今天在获取window中的button时,发现通过component无法获取,具体表现为: Ext.getCmp('loginForm').ownerCt.getComponent('btn_logi ...

  5. 从零开始学习MySQL1---MySQL基础

    数据库基础 数据库是一个长期存储在计算机内的.有组织的.有共享的.统一管理的.数据集合.它是一个按数据结构来存储和管理数据的计算机软件系统.数据库包含两层含义:保管数据的仓库,以及数据管理的方法和技术 ...

  6. Lwip lwip&lowbar;recvfrom函数一个数据包不能分多次读取。

    最近在写一个基于Lwip协议栈的网络程序,对于一包数据,想先获得包头信息,再根据包头信息读取后面的数据,但是调用recvfrom后,发现读取后面的数据读取不到,进一步查阅发现,原来对于UDP协议,一次 ...

  7. 清除浮动clearfix

    css用clearfix清除浮动 更多2013/11/4 来源:css学习浏览量:11901 学习标签: css clearfix 本文导读:写css 时总为浮动而烦恼,如果用了浮动,浮动的父层不会跟 ...

  8. AtCoder Regular Contest 077

    跟身在国外的Marathon-fan一起打的比赛,虽然最后没出F但还是涨分了. C - pushpush 题意:n次操作,每次往一个序列后面塞数,然后把整个序列翻转. #include<cstd ...

  9. git知识总结-4&period;git服务器搭建及迁移git仓库

    1. 前言 因为手里有一份代码之前是直接从其它git服务器上克隆下来的,现在想自己搭建一个git服务器把这份代码管起来. 2. 搭建git服务器 1.安装git: $ sudo apt-get ins ...

  10. WPF PrismDialog PopupWindowAction使用MetroWindow

    本示例必须在prism5.0版本以上 PopupWindowAction如何使用MetroWindow?   public class Window1ViewModel:BindableBase,II ...