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

题解:
考虑以牛为顶点的有向图,对每个需对(A,B)连一条从A到B的边。我们不妨假设两头牛A,B都被其他牛认为是红牛。那么就知道A,B一定同属一个强连通分量,即存在一个包含A,B两个顶点的圈。反之,如果一个牛被其他牛认为是红牛,那么他所属的强连通分量中的牛一定全部是红牛。所以我们只需要找出拓扑序最大的强连通分量的个数就可以了。 AC代码:
 #include<iostream>
#include<cctype>
using namespace std;
const int MAXN=+;
//-------------------------
void read(int &x){
x=;char ch=getchar();int f=;
for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-;
for(;isdigit(ch);ch=getchar())x=x*+ch-'';
x*=f;
}
//-------------------------
int n,m,tmp;
int topo[MAXN],cmp[MAXN];
bool vis[MAXN];
int first[MAXN],next[MAXN],v[MAXN],e;
void AddEdge(int a,int b){
v[++e]=b;
next[e]=first[a];
first[a]=e;
} int rfirst[MAXN],rnext[MAXN],rv[MAXN],re;
void rAddEdge(int a,int b){
rv[++re]=b;
rnext[re]=rfirst[a];
rfirst[a]=re;
}
//-------------------------
void dfs(int x){
vis[x]=;
for(int i=first[x];i;i=next[i])
if(!vis[v[i]])dfs(v[i]);
topo[++tmp]=x;
} void rdfs(int x,int k){
vis[x]=;
cmp[x]=k;
for(int i=rfirst[x];i;i=rnext[i])
if(!vis[rv[i]])rdfs(rv[i],k);
}
//---------------------------
int k=;
int scc(){
memset(vis,,sizeof(vis));
memset(topo,,sizeof(topo));
for(int i=;i<=n;i++){
if(!vis[i])dfs(i);
}
memset(vis,,sizeof(vis));
for(int i=n;i>=;i--)if(!vis[topo[i]])rdfs(topo[i],k++);
return k-;
}
//---------------------------
int main(){
read(n);read(m);
for(int i=;i<=m;i++){
int x,y;
read(x);read(y);
AddEdge(x,y);
rAddEdge(y,x);
}
int nn=scc(); int u=,num=;
for(int i=;i<=n;i++)
if(cmp[i]==nn){u=i;num++;}
memset(vis,,sizeof(vis));
rdfs(u,);
for(int i=;i<=n;i++)
if(!vis[i]){
num=;
break;
}
printf("%d\n",num);
}

												

Popular Cows (POJ No.2186)的更多相关文章

  1. Popular Cows(POJ 2186)

    原题如下: Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 40746   Accepted: 16 ...

  2. (连通图 缩点 强联通分支)Popular Cows -- poj --2186

    http://poj.org/problem?id=2186 Description Every cow's dream is to become the most popular cow in th ...

  3. Popular Cows POJ - 2186(强连通分量)

    Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= N <= 10, ...

  4. poj - 2186 Popular Cows && poj - 2553 The Bottom of a Graph (强连通)

    http://poj.org/problem?id=2186 给定n头牛,m个关系,每个关系a,b表示a认为b是受欢迎的,但是不代表b认为a是受欢迎的,关系之间还有传递性,假如a->b,b-&g ...

  5. poj 2186 Popular Cows (强连通分量+缩点)

    http://poj.org/problem?id=2186 Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissi ...

  6. POJ 2186 Popular Cows (强联通)

    id=2186">http://poj.org/problem? id=2186 Popular Cows Time Limit: 2000MS   Memory Limit: 655 ...

  7. poj 2186 Popular Cows 【强连通分量Tarjan算法 + 树问题】

    题目地址:http://poj.org/problem?id=2186 Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Sub ...

  8. 强连通分量分解 Kosaraju算法 (poj 2186 Popular Cows)

    poj 2186 Popular Cows 题意: 有N头牛, 给出M对关系, 如(1,2)代表1欢迎2, 关系是单向的且能够传递, 即1欢迎2不代表2欢迎1, 可是假设2也欢迎3那么1也欢迎3. 求 ...

  9. poj 2186 Popular Cows

    Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 29908   Accepted: 12131 De ...

随机推荐

  1. 在线提取PDF中图片和文字

    无需下载软件,你就可以在线提取PDF中图片和文字,http://www.extractpdf.com/不仅可以获取本地PDF文档的图片和文字,还能获取远程PDF文档的图片和文字.如下图所示:结果本人测 ...

  2. delphi if 语句循环语句

    if语句------------------------------------------------------------------------------------------------ ...

  3. 关于ASP.NET MVC中的视图生成

    在 ASP.NET MVC 中,我们将前端的呈现划分为三个独立的部分来实现,Controller 用来控制用户的操作,View 用来控制呈现的内容,Model 用来表示处理的数据.   从控制器到视图 ...

  4. SVM及其对偶

    引自 http://my.oschina.net/wangguolongnk/blog/111349 1. 支持向量机的目的是什么? 对于用于分类的支持向量机来说,给定一个包含正例和反例(正样本点和负 ...

  5. Android系统源代码下载

    最近,我在研究android,所以想下载android源代码看看.按照http://source.android.com/source/downloading.html 这个页面所提示的步骤在下载源代 ...

  6. Spark 资源调度及任务调度

    1.  资源分配 通过SparkSubmit进行提交应用后,首先会创建Client将应用程序(字节码文件.class)包装成Driver,并将其注册到Master.Master收到Client的注册请 ...

  7. Linux C 调用MYSQL API 函数mysql_escape_string()转义插入数据

    Title:Linux C 调用MYSQL API 函数mysql_escape_string()转义插入数据 --2013-10-11 11:57 #include <stdio.h> ...

  8. android百度定位

    package com.aihunqin.test; import android.app.Activity; import android.os.Bundle; import android.wid ...

  9. QTableWidget 用法总结(只能使用标准的数据模型,并且其单元格数据是QTableWidgetItem的对象)

    QTableWidget是QT程序中常用的显示数据表格的空间,很类似于VC.C#中的DataGrid.说到QTableWidget,就必须讲一下它跟QTabelView的区别了.QTableWidge ...

  10. Java如何让异常处理机制更完备规范

    1)catch的Exception一定要详细的点名是某种异常而非一概而论的用Exception ex来接收所有的异常,往往不理解这点的人也不能很好的理解catch的意义到底在哪里,是对捕获的异常进行一 ...