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. google play iap 常见问题

    1.测试阶段query时获取的sku对象为空 解:测试阶段只能使用如下sku // private static final String SKU_TEST = "android.test. ...

  2. Runtime实战之定制TabBarItem大小

    方案一:UIEdgeInsets 适用场景: 适合APP的TabBarItemImage的图片资源放在本地 图片超出tabbar的高度,需移动其位置,来进行适应 弊端: 若在本地配置好后,tabbar ...

  3. CSS 的定位方式和含义

    CSS 的定位方式和含义 总结一下 CSS 的定位方式.CSS 的定位 position 是处理页面布局时非常重要的属性. CSS 中有 3 种基本的定位机制:普通流.浮动和绝对定位. 在没有指定的情 ...

  4. C#通过SSH连接MySql

    出于安全考虑,有的时候数据库服务器只能通过SSH访问,比如MySql服务装在了服务器A上,并且A机器只允许B机器才能访问,而部署环境可能在C机器上,这时候就要C服务器通过B服务器连接A服务器,这时候就 ...

  5. Linux -- objdump二进制文件比较

    objdump工具用来显示二进制文件的信息,就是以一种可阅读的格式让你更多地了解二进制文件可能带有的附加信息. 常用参数说明 -f 显示文件头信息 -D 反汇编所有section (-d反汇编特定se ...

  6. vue.js在windows本地下搭建环境和创建项目

    Vue.js是一套构建用户界面的渐进式框架.与其他重量级框架不同的是,Vue 采用自底向上增量开发的设计.Vue 的核心库只关注视图层,并且非常容易学习,非常容易与其它库或已有项目整合.另一方面,Vu ...

  7. JavaScript基本操作

    一.如何编写? 1.JavaScript代码存在形式 <!-- 方式一 --> <script type="text/javascript" src=" ...

  8. Android 自定义View (五)——实践

    前言: 前面已经介绍了<Android 自定义 view(四)-- onMeasure 方法理解>,那么这次我们就来小实践下吧 任务: 公司现有两个任务需要我完成 (1)监测液化天然气液压 ...

  9. sed tr 去除PATH中的重复项

    最近发现由于自己不良的安装软件的习惯,shell的PATH路径包含了很多冗余的项.这里使用shell命令去除PATH的冗余项. export PATH=$(echo $PATH | sed 's/:/ ...

  10. rqt工具的使用

    安装rqt工具sudo apt-get install ros-indigo-rqtsudo apt-get install ros-indigo-rqt-common-plugins或者rosdep ...