Time Limit: 2000MS   Memory Limit: 65536K
     

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

USACO 2003 Fall



题意:每一个奶牛都想成为牧群中最受仰慕的奶牛,在牧群中有n头奶牛,给定m对关系(A,B),表示A奶牛仰慕B奶牛。计算牧群中被其他奶牛仰慕的奶牛的数目。



思路:将n头奶牛的m个关系将会构建一个有向图,在图中强连通分量中的任意奶牛一定是被分量中的其他奶牛仰慕。所以问题就转化为在图中将强连通分量进行缩点,在形成的新图中,如果一个强连通分量集合的出度为零,说明这个集合被其他集合仰慕,而不仰慕其他的集合,所以如果在新图中集合出度为零的数目不大于1,则为出度为零集合中奶牛的数目,如果大于1,则出度为零集合之间没有仰慕关系的,所以结果为0.



#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm> using namespace std; const int Max = 10100; typedef struct node
{
int v; int next; }Line; vector<int>G[Max]; Line Li[Max*5]; int Head[Max],top; int dfn[Max],low[Max],pre[Max]; int num[Max],Num,Du[Max]; int dep; bool vis[Max]; int ans; stack<int>S; void AddEdge(int u,int v)
{
Li[top].v=v ; Li[top].next=Head[u]; Head[u]=top++;
} void Tarjan(int u)// Tarjan求强连通分量
{
dfn[u]=low[u]=dep++; S.push(u); for(int i=Head[u];i!=-1;i=Li[i].next)
{
if(dfn[Li[i].v]==-1)
{
Tarjan(Li[i].v); low[u]=min(low[u],low[Li[i].v]);
}
else
{
low[u]=min(low[u],dfn[Li[i].v]);
}
} if(low[u]==dfn[u])//强连通分量的根节点
{ while(!S.empty())
{
int v = S.top(); S.pop(); pre[v] = Num;//给分量集合标号 G[Num].push_back(v);//记录集合对应的点 num[Num]++; if(v==u)
{
break;
}
} Num++;
} } int main()
{ int n,m; while(~scanf("%d %d",&n,&m))
{
top = 0; memset(Head,-1,sizeof(Head)); int u,v; for(int i=0;i<m;i++)
{
scanf("%d %d",&u,&v); AddEdge(u,v);
} memset(dfn,-1,sizeof(dfn)); memset(num,0,sizeof(num)); for(int i =0;i<=n;i++)
{
G[i].clear();
} dep = 0;Num = 0; for(int i=1;i<=n;i++)
{
if(dfn[i]==-1)
{
Tarjan(i);
}
} memset(Du,0,sizeof(Du)); for(int i=0; i<Num ;i++) //判断强连通分量的出度
{
memset(vis,false,sizeof(vis)); for(int k=0;k<G[i].size();k++)
{
for(int j=Head[G[i][k]]; j!=-1;j=Li[j].next)
{
if(i!=pre[Li[j].v])
{
if(!vis[pre[Li[i].v]])//由于不同的强连通分量之间相连的边可能不止一条,所以要判断是不是已经统计过。
{
vis[pre[Li[i].v]]=true; Du[i]++;
}
}
}
}
} ans = 0; int ant = 0; for(int i=0;i<Num;i++)//如果超过一个的出度为零的强连通分量,则这些连通分量就不能被其他的牛都仰慕。
{
if(Du[i]==0)
{
ans+=num[i]; ant++; }
} printf("%d\n",ant<=1?ans:0); } return 0;
}

Popular Cows-POJ2186Tarjan的更多相关文章

  1. POJ 2186 Popular Cows(Targin缩点)

    传送门 Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 31808   Accepted: 1292 ...

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

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

  3. poj 2186 Popular Cows

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

  4. [强连通分量] POJ 2186 Popular Cows

    Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 31815   Accepted: 12927 De ...

  5. POJ 2186 Popular Cows(强连通)

                                                                  Popular Cows Time Limit: 2000MS   Memo ...

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

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

  7. poj 2186 Popular Cows【tarjan求scc个数&&缩点】【求一个图中可以到达其余所有任意点的点的个数】

    Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 27698   Accepted: 11148 De ...

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

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

  9. POJ 2186 Popular Cows (强联通)

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

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

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

随机推荐

  1. Nginx+Tomcat+Redis实现负载均衡、资源分离、session共享

    Nginx+Tomcat+Redis实现负载均衡.资源分离.session共享 CentOS安装Nginx http://centoscn.com/CentosServer/www/2013/0910 ...

  2. Glide加载异常调试

    Glide提供了listener链式调用方法,传入一个RequestListener对象,然后实现内部的onException方法,那么在Glide加载图片失败时就会调用这个方法. 代码实现很简单: ...

  3. C#代码

    http://www.cnblogs.com/zjfree/category/269738.html 超简易静态Web服务器 C# 生成不重复随机字符串 (1秒内生成1000000个) C# 读写IN ...

  4. Selenium2学习-039-WebUI自动化实战实例-文件上传下载

    通常在 WebUI 自动化测试过程中必然会涉及到文件上传的自动化测试需求,而开发在进行相应的技术实现是不同的,粗略可划分为两类:input标签类(类型为file)和非input标签类(例如:div.a ...

  5. Hibernate操作指南-搭建一个简单的示例(基于Java Persistence API JPA)

  6. spark 源码安装

    clone 源码 git clone git://github.com/apache/spark.git maven编译源码 国外镜像比较慢,此处修改maven仓库的镜像为阿里云镜像: <mir ...

  7. Myeclipse8.6配置android_SDK,进行android开发(转载)

    Myeclipse8.6下部署环境,总结一下个人Android环境配置的过程(首先不要急着启动Myeclipse) 1.下载SDK:http://developer.android.com/sdk/i ...

  8. C#并行编程之数据并行

    所谓的数据并行的条件是: 1.拥有大量的数据. 2.对数据的逻辑操作都是一致的. 3.数据之间没有顺序依赖. 运行并行编程可以充分的利用现在多核计算机的优势.记录代码如下: public class ...

  9. No compiler is provided in this environment. Perhaps you are running on a JRE ra

    No compiler is provided in this environment. Perhaps you are running on a JRE ra,有需要的朋友可以参考下. 控制台输出的 ...

  10. Linux操作系统备份之三:通过二进制拷贝(dd)方式实现Linux操作系统数据的备份

    前面有两篇文章,<Linux操作系统备份之一:使用LVM快照实现Linux操作系统数据的在线备份>和<Linux操作系统备份之二:通过tar拷贝分区实现Linux操作数据的在线备份& ...