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. PHP连续签到

    require "./global.php"; $act = isset($_GET['act']) ? $_GET['act'] : "error"; // ...

  2. 深入浅出Java 重定向和请求转发的区别

    深入浅出Java 重定向和请求转发的区别 <span style="font-family:FangSong_GB2312;font-size:18px;">impor ...

  3. 如何使用sublime编辑器运行python程序

    现在越发喜欢sublime编辑器了,不仅界面友好美观.文艺,可扩展性还特别强. sublime本身是不具备运行python程序的能力的,需要做些设置才可以.以下是安装好sublime后设置的步骤: 点 ...

  4. CSS3 加载进度样式

    <html> <head> <style type="text/css"> body{ background-color: green; } . ...

  5. Hdu 1059 Dividing & Zoj 1149 & poj 1014 Dividing(多重背包)

    多重背包模板- #include <stdio.h> #include <string.h> int a[7]; int f[100005]; int v, k; void Z ...

  6. linux开源论坛

    开源资源: 开源http://oss.org.cn/?action-news http://www.lupaworld.com/proj.php http://www.10pig.cn/linux/o ...

  7. Java中string拼接,StringBuilder,StringBuffer和+

    Java中string拼接,StringBuilder,StringBuffer和+,到底哪个更合适? StringBuilder线程不安全,效率较线程安全的StringBuffer高.jdk1.5之 ...

  8. 安装与使用smarty

    1.安装 下载最新的smarty.下载地址:http://www.smarty.net/download 下载成功后,解压压缩包后的文件如图所示: 将解压后的文件存放在web文档根目录外的某个位置.w ...

  9. Codeforces 235E Number Challenge

    http://codeforces.com/contest/235/problem/E 远距离orz......rng_58 证明可以见这里(可能要FQ才能看到) 还是copy一下证明吧: 记 $$f ...

  10. AngularJs中文社区学习资料

    AngularJs中文社区学习资料,供学习: http://angularjs.cn/tag/AngularJS