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. 

Source

 
 
正解:tarjan+缩环成点
解题报告:
  有一周(4天)没写题了,马上期末考试了,一直在搞学科,但是还是想刷道题保持手感。。。考完期末就可以搞一个暑假竞赛了
  题目大意是给定一个有向图,找到所有点都能到达的点的个数。
  一眼秒题,tarjan算法,然后缩环,发现只有一个出边为0的环,则环中所有点都是合法解;如果不止一个,那么无解。
  POJ上交了一发,一遍AC,手感还不错。
 //It is made by jump~
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#ifdef WIN32
#define OT "%I64d"
#else
#define OT "%lld"
#endif
using namespace std;
typedef long long LL;
const int MAXN = ;
const int MAXM = ;
int n,m;
int ecnt,cnt,total;
int first[MAXN],next[MAXM],to[MAXM];
int belong[MAXN];
int dfn[MAXN],low[MAXN];
bool pd[MAXN];
int Stack[MAXN],top;
int out[MAXN];
int ans,jilu; inline int getint()
{
int w=,q=;
char c=getchar();
while((c<'' || c>'') && c!='-') c=getchar();
if (c=='-') q=, c=getchar();
while (c>='' && c<='') w=w*+c-'', c=getchar();
return q ? -w : w;
} inline void Init(){
ecnt=; cnt=; total=; top=;
memset(first,,sizeof(first));
memset(pd,,sizeof(pd));
memset(dfn,,sizeof(dfn));
memset(Stack,,sizeof(Stack));
} inline void link(int x,int y){
next[++ecnt]=first[x]; first[x]=ecnt; to[ecnt]=y;
} inline void dfs(int x){
dfn[x]=++total; low[x]=dfn[x];
pd[x]=;
Stack[++top]=x;
for(int i=first[x];i;i=next[i]) {
int v=to[i];
if(!dfn[v]) {
dfs(v);
low[x]=min(low[x],low[v]);
}
else if(pd[v] && low[v]<low[x]) low[x]=low[v];
}
if(dfn[x]==low[x]) {
cnt++;
int now=Stack[top];
do{
belong[now]=cnt;
pd[now]=; top--;
if(now==x) break;
now=Stack[top];
}while();
}
} inline void tarjan(){
for(int i=;i<=n;i++) if(!dfn[i]) dfs(i);
} inline void solve(){
while(scanf("%d%d",&n,&m)!=EOF) {
Init();
int x,y;
for(int i=;i<=m;i++) {
x=getint(); y=getint();
link(x,y);
}
tarjan();
for(int i=;i<=n;i++){
for(int j=first[i];j;j=next[j]) {
int v=to[j];
if(belong[v]!=belong[i]) {
out[belong[i]]++;
}
}
}
ans=,jilu=;
for(int i=;i<=cnt;i++){
if(out[i]==) {
ans++; jilu=i;
}
}
if(ans==) {
ans=;
for(int i=;i<=n;i++) {
if(belong[i]==jilu) ans++;
}
printf("%d\n",ans);
}
else printf("0\n");
}
} int main()
{
solve();
return ;
}

POJ2186 Popular Cows的更多相关文章

  1. 强连通分量tarjan缩点——POJ2186 Popular Cows

    这里的Tarjan是基于DFS,用于求有向图的强联通分量. 运用了一个点dfn时间戳和low的关系巧妙地判断出一个强联通分量,从而实现一次DFS即可求出所有的强联通分量. §有向图中, u可达v不一定 ...

  2. 洛谷——P2341 [HAOI2006]受欢迎的牛//POJ2186:Popular Cows

    P2341 [HAOI2006]受欢迎的牛/POJ2186:Popular Cows 题目背景 本题测试数据已修复. 题目描述 每头奶牛都梦想成为牛棚里的明星.被所有奶牛喜欢的奶牛就是一头明星奶牛.所 ...

  3. POJ2186 Popular Cows [强连通分量|缩点]

    Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 31241   Accepted: 12691 De ...

  4. POJ2186 Popular Cows 【强连通分量】+【Kosaraju】+【Tarjan】+【Garbow】

    Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 23445   Accepted: 9605 Des ...

  5. 【Tarjan缩点】POJ2186 Popular Cows

    Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 35644   Accepted: 14532 De ...

  6. poj2186 Popular Cows 题解——S.B.S.

    Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 29642   Accepted: 11996 De ...

  7. POJ-2186 Popular Cows,tarjan缩点找出度为0的点。

    Popular Cows 题意:一只牛崇拜另外一只牛,这种崇拜关系可以传导.A->B,B->C =>A->C.现在给出所有的关系问你有多少牛被其他所有的牛都崇拜. 思路:就是一 ...

  8. POJ2186 Popular Cows(强连通分量)

    题目问一个有向图所有点都能达到的点有几个. 先把图的强连通分量缩点,形成一个DAG,那么DAG“尾巴”(出度0的点)所表示的强连通分量就是解,因为前面的部分都能到达尾巴,但如果有多个尾巴那解就是0了, ...

  9. POJ2186 Popular Cows 强连通分量tarjan

    做这题主要是为了学习一下tarjan的强连通分量,因为包括桥,双连通分量,强连通分量很多的求法其实都可以源于tarjan的这种方法,通过一个low,pre数组求出来. 题意:给你许多的A->B ...

随机推荐

  1. java 20 - 6 加入了异常处理的字节输出流的操作

    昨天坐了十几个钟的车回家,累弊了.... ————————————割掉疲劳————————————— 前面的字节输出流都是抛出了异常不管,这次的加入了异常处理: 首先还是创建一个字节输出流对象,先给它 ...

  2. View (二) 自定义属性

    主要有三种方法可以实现自定义属性. 方法一:不使用命名空间,不使用attrs.xml文件.通过attrs.getAttributeResourceValue方法拿到属性值 方法二: 使用命名空间, 不 ...

  3. linux下的缓存机制及清理buffer/cache/swap的方法梳理

    (1)缓存机制 为了提高文件系统性能,内核利用一部分物理内存分配出缓冲区,用于缓存系统操作和数据文件,当内核收到读写的请求时,内核先去缓存区找是否有请求的数据,有就直接返回,如果没有则通过驱动程序直接 ...

  4. 分布式监控系统Zabbix-3.0.3-完整安装记录(4)-解决zabbix监控图中出现中文乱码问题

    之前部署了Zabbix-3.0.3监控系统,在安装数据库时已经将zabbix库设置了utf-8字符. 首先确定zabbix开启了中文支持功能:登录到zabbix服务器的数据目录下(前面部署的zabbi ...

  5. iOS -数据库网络之xml解析之第三方解析XML

    1.导入第三方插件(GDalaXMLNode)   2.第三方插件配置   libxml/tree.h 路径   在项目属性中--Bulid Settings中搜索 Search   --Search ...

  6. 基于RESTful标准的Web Api

    微软的web api是在vs2012上的mvc4项目绑定发行的,它提出的web api是完全基于RESTful标准的,完全不同于之前的(同是SOAP协议的)wcf和webService,它是简单,代码 ...

  7. JS结合DOM事件的例子

    // 这是初始文字 右边是一个测试文本框: 鼠标划过.点击.松开上面的文字都会有不同的效果,鼠标光标移到.离开文本框也会有不同的效果. 首先新建一个html文件 <!DOCTYPE html&g ...

  8. 在opencv3中实现机器学习之:利用正态贝叶斯分类

    opencv3.0版本中,实现正态贝叶斯分类器(Normal Bayes Classifier)分类实例 #include "stdafx.h" #include "op ...

  9. java系列:《java核心技术 卷1》学习笔记,chapter 11 调试技巧

    11. 6 调试技巧 1)一个不太为人所知却非常有效的技巧是在每个类中放一个main方法,这样就可以对每个类进行单元测试.这个方法可以保留,因为在java虚拟机只调用启动类的main方法. 2)   ...

  10. iOS如何上传代码到Github

    iOS如何上传代码到Github 很多iOS开发者想开源自己的代码或者demo,开源到Github是个不错的选择,那么如何上传我们的代码到Github,令所有人可以下载使用呢?这里我们的目的很明确,就 ...