Popular Cows
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 31533   Accepted: 12817

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,那么就有A欢迎C。我们要找到能让其他所有的牛都欢迎的牛。
  (废话真多)
  就是求强连通分量了,然后找到一个强连通分量,缩点,最后找出度为0的点(这里可以自己草纸划一下),如果有多个出度为0的点,那么肯定没有牛被所有牛欢迎,如果只有一个,输出这个点所代表的强连通分量的长度!!!md
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
const int maxn=;
vector<int>tu[maxn];
vector<int>lt[maxn];
int n,m,lts=;
int js=;
int dfn[maxn],low[maxn];
int zhan[maxn],top=;
bool isins[maxn];
int num[maxn];//num[i]表示i点所在的强连通分量的编号
int d[maxn];
void tarjan(int i)//hhhh
{
int j;
dfn[i]=low[i]=++js;
isins[i]=;
zhan[top++]=i;
for(int j=;j<tu[i].size();j++)
{
int tp=tu[i][j];
if(dfn[tp]==-)
tarjan(tp),
low[i]=min(low[i],low[tp]);
else if(isins[tp])
low[i]=min(low[i],dfn[tp]);
}
if(dfn[i]==low[i])
{
lts++;
do{
j=zhan[--top];
isins[j]=;
lt[lts].push_back(j);
num[j]=lts;//j点所在的强连通分量的编号为lts
}while(i!=j);
}
}
void solve(int n)
{
memset(dfn,-,sizeof dfn);
memset(low,-,sizeof low);
memset(zhan,-,sizeof zhan);
memset(isins,,sizeof isins);
for(int i=;i<n;i++)
if(dfn[i]==-)
tarjan(i);
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
tu[x-].push_back(y-);//我从0开始啊
}
solve(n);
for(int i=;i<n;i++)
for(int j=;j<tu[i].size();j++)
{
int t=tu[i][j];
if(num[i]!=num[t])//如果i点与他指向的点不在同一个强连通分量中
d[num[i]]++;//i点所在的强连通分量的出度+1
}
int pos=-;
int cnt=;
for(int i=;i<=lts;i++)
if(d[i]==)//找到出度为0的强连通分量
cnt++,
pos=i;
if(cnt==) cout<<lt[pos].size();//如果只有一个出度为0的强连通分量,那么这个强连通分量的长度即答案
else cout<<"";//woc
return ;
}

Poj2186Popular Cows的更多相关文章

  1. poj2186Popular Cows(Kosaraju算法--有向图的强连通分量的分解)

    /* 题目大意:有N个cows, M个关系 a->b 表示 a认为b popular:如果还有b->c, 那么就会有a->c 问最终有多少个cows被其他所有cows认为是popul ...

  2. 图论:POJ2186-Popular Cows (求强连通分量)

    Popular Cows Description Every cow's dream is to become the most popular cow in the herd. In a herd ...

  3. poj--2186--Popular Cows (scc+缩点)

    Popular Cows Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total ...

  4. poj2186Popular Cows(强连通分量)

    http://poj.org/problem?id=2186 用tarjan算出强连通分量的个数 将其缩点 连成一棵树  则题目所求即变成求出度为0 的那个节点 在树中是唯一的 即树根 #includ ...

  5. poj2186-Popular Cows【Tarjan】+(染色+缩点)(经典)

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

  6. poj2186-Popular Cows(强连通分支)

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

  7. POJ-2186-Popular Cows(强连通分量,缩点)

    链接:https://vjudge.net/problem/POJ-2186 题意: 有N(N<=10000)头牛,每头牛都想成为most poluler的牛,给出M(M<=50000)个 ...

  8. poj2186Popular Cows+tarjan缩点+建图

    传送门: 题意: 给出m条关系,表示n个牛中的崇拜关系,这些关系满足传递性.问被所有牛崇拜的牛有几头: 思路: 先利用tarjan缩点,同一个点中的牛肯定就是等价的了,建立新的图,找出其中出度为0的点 ...

  9. [LeetCode] Bulls and Cows 公母牛游戏

    You are playing the following Bulls and Cows game with your friend: You write a 4-digit secret numbe ...

随机推荐

  1. SQL的四种连接-左外连接、右外连接、内连接、全连接

    今天在看一个遗留系统的数据表的时候发现平时查找的视图是FULL OUT JOIN的,导致平时的数据记录要进行一些限制性处理,其实也可以设置视图各表为右外连接并在视图上设置各列的排序和筛选条件就可以达到 ...

  2. WordPress基础:自定义菜单

    需要自定义一个菜单,可以访问后台->外观->菜单

  3. h5移动端常见问题

    meta基础知识 H5页面窗口自动调整到设备宽度,并禁止用户缩放页面 1 <meta name="viewport" content="width=device-w ...

  4. C++学习笔记 四种新式类型转换

    static_cast ,dynamic_cast,const_cast,reinterpret_cast static_cast 定义:通俗的说就是静态显式转换,用于基本的数据类型转换,及指针之间的 ...

  5. Memcached,你懂的

    一.Memcached简介 Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网 ...

  6. pycharm licenseserver 注册方法

    pycharm5.0之后,以前的很多注册码都无法使用,可以选择使用license server 方式进行注册,方法如下: 注册方法:    在 注册时选择 License server ,填 http ...

  7. mapreduce性能提升2

    mapreduce性能提升2mapreduce性能提升2mapreduce性能提升2

  8. Android中使用Handler造成内存泄露的分析和解决

    什么是内存泄露?Java使用有向图机制,通过GC自动检查内存中的对象(什么时候检查由虚拟机决定),如果GC发现一个或一组对象为不可到达状态,则将该对象从内存中回收.也就是说,一个对象不被任何引用所指向 ...

  9. Js动态获取iframe子页面的高度////////////////////////zzzz

    Js动态获取iframe子页面的高度   Js动态获取iframe子页面的高度总结 问题的缘由 产品有个评论列表引用的是个iframe,高度不固定于是引发这个总结. 方法1:父级页面获取子级页面的高度 ...

  10. Unit01: JAVA开发环境案例

    Top JAVA Fundamental DAY01 JDK及Eclipse目录结构操作 JDK的安装及配置 控制台版的JAVA HelloWorld 使用Eclipse开发Java应用程序 1 JD ...