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 - 7 字节输入流的操作

    字节输入流操作步骤: A:创建字节输入流对象 B:调用read()方法读取数据,并把数据显示在控制台 C:释放资源 步骤A.C 略过,说步骤B  读取数据的方式:  A:int read():一次读取 ...

  2. smarty变量调节器

    smarty中变量调解器的作用:在模板中需要对PHP分配过来的变量在输出之前,对变量进行处理 注册变量调解器方式:$smarty->registerPlugin("modifier&q ...

  3. Myeclipse+maven时Tomcat部署时maven的依赖文件不能部署到Tomcat上

    解决办法:

  4. RelayCommand命令

    原文:http://www.cnblogs.com/xiepeixing/archive/2013/08/13/3255152.html 常用Wpf开发中我们在ViewModel中实现INotifyP ...

  5. Linux中查看各文件夹大小命令du -h --max-depth=1

    http://blog.csdn.net/ouyang_peng/article/details/10414499 du -h --max-depth=1  

  6. 那些OVER的封装

    什么over什么,如pppoe, ppp的封装都在over对象之后,入下图: PPPOE   Ipsec

  7. C语言 复杂的栈(链表栈)

    //复杂的栈--链表栈 #include<stdio.h> #include<stdlib.h> #define datatype int//定义链表栈数据类型 //定义链表栈 ...

  8. Unity架构有点乱

    1,没有合理的将公共的东西归入到基类中,而是分散到子类中,有许多重复. 比如 enbled的变量本应该是所有component所共有的一个属性,应该写在component.然而却发现并非这样,enbl ...

  9. WireShark系列: 使用WireShark过滤条件抓取特定数据流(zz)

    应用抓包过滤,选择Capture | Options,扩展窗口查看到Capture Filter栏.双击选定的接口,如下图所示,弹出Edit Interface Settints窗口. 下图显示了Ed ...

  10. Jquery each() 如何操作动态添加的DOM元素

    JQ each页面上的DOM元素的时候,如果有一部分的DOM是ajax异步加载的,那么each可能遍历不到元素,异步请求数据的同时不会影响后面的代码执行,今天就遇到这个问题了, 解决方案是讲ajax改 ...