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. Android优化——UI优化(一)优化布局层次

    优化布局层次 1.避免布局镶嵌过深(如下) <LinearLayout xmlns:android="http://schemas.android.com/apk/res/androi ...

  2. <<Effective Java>>之Comparable接口的实现约定

    对于BigDecimal类在HashSet和TreeSet中 new BigDecimal("1.00") new BigDecimal("1.0") 在Has ...

  3. Linux ssh登录和软件安装详解

    阿哲Style   Linux第一天 ssh登录和软件安装详解 Linux学习第一天 操作环境: Ubuntu 16.04 Win10系统,使用putty_V0.63 本身学习Linux就是想在服务器 ...

  4. C语言 复杂队列(链表队列)

    //复杂的队列二 --链表队列 #include<stdio.h> #include<stdlib.h> #define datatype int struct queueli ...

  5. web页面的回流,认识与避免

    一.什么是回流? 回流是会导致页面重新渲染的一些元素,从而影响性能. 二.哪些因素会导致回流? 1.调整窗口的大小: 2.改变字体,如果用rem  设置了根目录的字体大小,这样就减少了回流的次数: 3 ...

  6. GeoServer 常见问题总结

    Geoserver安装环境 Geoserver在部署发布服务时,经常会遇到如下问题,现总结如下: 1.忘记了GeoServer Web Admin Page的登陆用户名和密码怎么办? 存储位置:C:\ ...

  7. iBatis.Net(C#)SQL数据映射

    转载请注明 http://www.cnblogs.com/13590/archive/2013/03/01/2938126.html 摘要:本文探讨了iBatis.Net框架的XML数据映射文件各配置 ...

  8. [CareerCup] 12.3 Test Move Method in a Chess Game 测试象棋游戏中的移动方法

    12.3 We have the following method used in a chess game: boolean canMoveTo( int x, int y). This metho ...

  9. 《Linux内核设计与实现》 Chapter4 读书笔记

    <Linux内核设计与实现> Chapter4 读书笔记 调度程序负责决定将哪个进程投入运行,何时运行以及运行多长时间,进程调度程序可看做在可运行态进程之间分配有限的处理器时间资源的内核子 ...

  10. 创建Maven工程

    一.Maven工程创建 File->New->Other,进入: 点击Next,进入: 勾选上Create a simple project(不使用骨架) 点击Next,进入: 输入项目名 ...