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

最新文章

  1. CentOS系统配置 iptables防火墙
  2. slf4j log4j logback关系详解和相关用法
  3. 装完RHEL7后,重新开机启动后显示:Initial setup of CentOS Linux 7 (core) 提示license报错
  4. linux建立用户 详细
  5. 【¥200代金券、iPad等您来拿】 阿里云9大产品免费公测#10月9日-11月6日#
  6. SpeedPHP关于一对一和一对多关联关系的建立 model建立
  7. Linux基本配置和管理 2 ---- Linux多命令协作----管道及重定向
  8. ubuntu 开启 rewrite 模块
  9. hadoop笔记之Hive的数据类型
  10. linux下getsockopt和setsockopt具体解释及測试
  11. tab切换☆☆☆☆☆
  12. 201521123053《Java程序设计》第3周学习总结
  13. 》》豆瓣API
  14. mac攻略(3) -- brew使用
  15. 原生js简单调用百度翻译API实现的翻译工具
  16. Winfrom 动画实现
  17. 实验六 MapReduce实验:二次排序
  18. LOJ.6062.[2017山东一轮集训]Pair(Hall定理 线段树)
  19. myeclipse新建maven项目
  20. 今天清理C盘空间,发现Unity的一个问题

热门文章

  1. GCD的简单用法
  2. CentOS6.6安装virtualbox4.1.44
  3. RDIFramework.NET ━ .NET快速信息化系统开发框架 ━ 工作流程组件介绍
  4. ubuntu16.04部署RED5流媒体服务器
  5. struts-标签
  6. Centos 7 mysql Buffered warning: Changed limits: max_connections: 214 解决方法
  7. [转]Linux/Ubuntu sudo不用输入密码的方法
  8. 搭建S3C6410开发板的测试环境
  9. Qt4.8.5在ARM9上的移植
  10. Java被忽略的基本知识(四)