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. .NET中MemCached使用介绍

    阅读目录 1.MemCached是什么? 2.Window中MemCached安装 3.MemCached命令 4.简单示例 MemCached是什么 MemCached是一个自由开源,高性能,分布式 ...

  2. Ubuntu 14.04 编译安装 boost 1.58

    简介 Boost is a set of libraries for the C++ programming language that provide support for tasks and s ...

  3. C#中Object和Json之间的转换

    //创建一个类,此类有两个方法 using Newtonsoft.Json;namespace System{    public static class JsonExtension    {    ...

  4. 简单的js菜单

    <!DOCTYPE html> <html> <head> <title>hovertree</title><base target= ...

  5. Spring事务传播属性

    Spring 对事务控制的支持统一在 TransactionDefinition 类中描述,该类有以下几个重要的接口方法: int getPropagationBehavior():事务的传播行为 i ...

  6. hp-pa安装oracle和bash

    一.安装oracle数据库 安装之前先进行环境检查: 1.检查系统版本 #uname -a 2.查看内存大小 #/usr/contrib/bin/machinfo | grep -i Memory 3 ...

  7. MultiWiiWinGUI 汉化版

    已经基本汉化完毕 下载

  8. android nfc中MifareClassic格式的读写

    Android支持的数据格式 数据格式的Intent filter AndroidManifest.xml文件中,要像向下列示例那样,在<activity>元素内的<meta-dat ...

  9. ASCII码表(二进制 十进制 十六进制)

    css里,允许使用转义字符\+ascii16进制形式; 例如: e的ascii 16进制是65,我们就写为\65 expression -> expr\65ssion 二进制 十进制 十六进制 ...

  10. swift基础:第四部分:对函数和闭包的深入

    ()之前在什么公司,都自己做过哪些项目,从架构的角度来谈谈你的项目. () 你对iOS不同版本是怎么看的,你在做项目的过程当中,是如何应对版本问题的. () 你对iOS的性能是怎么优化的. () 你通 ...