poj2186 popular cows
 
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

题意:10000头奶牛,50000对仰慕关系,且仰慕关系可以传递,问所有牛都仰慕的对象有多少?

————————————————————————————————————————————————

强连通分量中相互仰慕,所点后各点形成有向无环图,而其中如果有一个点(强连通分量)出度为0,那么这个强连通分量包含的点数就是答案;若果有多个点出度为0,则没有所有牛都仰慕的牛,答案0。

————————————————————————————————————————————————

 //poj2186 popular cows
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<stack> using namespace std;
const int maxn=;
const int maxm=;
int n,m;
struct edge
{
int u,v,next;
}e[maxm],ee[maxm];
int head[maxn],js,headd[maxn],jss;
int dfsn[maxn],low[maxn],belong[maxn];
bool ins[maxn];
int sshu;
int chudu[maxn],ss[maxn];
stack<int>st;
int visx;
void init()
{
memset(head,,sizeof(head));
memset(e,,sizeof(e));
js=;
memset(headd,,sizeof(headd));
memset(ee,,sizeof(ee));
jss=;
memset(dfsn,-,sizeof(dfsn));
memset(low,-,sizeof(low));
sshu=;
memset(ss,,sizeof(ss));
while(!st.empty())st.pop();
visx=;
memset(ins,,sizeof(ins));
memset(chudu,,sizeof(chudu));
memset(belong,,sizeof(belong));
}
void addage(int u,int v,edge e[],int head[],int &js)
{
e[++js].u=u;e[js].v=v;
e[js].next=head[u];head[u]=js;
} void tarjan(int u)
{
dfsn[u]=low[u]=++visx;
st.push(u);
ins[u]=;
for(int i=head[u];i;i=e[i].next)
{
int v=e[i].v;
if(dfsn[v]==-)
{
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(ins[v] && low[u]>dfsn[v])low[u]=dfsn[v];
}
if(low[u]==dfsn[u])
{
sshu++;
int tp;
do
{
tp=st.top();
st.pop();
ins[tp]=;
ss[sshu]++;
belong[tp]=sshu;
}while(u!=tp);
}
}
int main()
{
while(scanf("%d%d",&n,&m)==)
{
init();
for(int i=,u,v;i<m;i++)
{
scanf("%d%d",&u,&v);
addage(u,v,e,head,js);
}
for(int i=;i<=n;i++)
if(dfsn[i]==-)tarjan(i);
for(int i=;i<=m;i++)
{
int u=e[i].u,v=e[i].v;
if(belong[u]!=belong[v])
{
addage(belong[u],belong[v],ee,headd,jss);
chudu[belong[u]]++;
}
}
int count=,mn;
for(int i=;i<=sshu;i++)
if(chudu[i]==)count++,mn=i;
if(count==)printf("%d\n",ss[mn]);
else printf("0\n");
}
return ;
}

POJ2186的更多相关文章

  1. 【poj2186】 Popular Cows

    http://poj.org/problem?id=2186 (题目链接) 题意 给出一个n个点m条边的有向图,求其中没有出度强连通分量所包含的点有几个 Solution 其实这道题的题解已经在“题意 ...

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

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

  3. 不裸缩点》。。。POJ2186受欢迎的牛

    不裸缩点>...POJ2186受欢迎的牛 :first-child { margin-top: 0; } blockquote > :last-child { margin-bottom: ...

  4. 最近切的两题SCC的tarjan POJ1236 POJ2186

    两题都是水题,1236第一问求缩点后入度为0的点数,第二问即至少添加多少条边使全图强连通,属于经典做法,具体可以看白书 POJ2186即求缩点后出度为0的那个唯一的点所包含的点数(即SCC里有多少点) ...

  5. 强连通分量+poj2186

    强连通分量:两个点能够互相连通. 算法分解:第一步.正向dfs全部顶点,并后序遍历 第二步,将边反向,从最大边dfs,构成强连通分量 标号最大的节点属于DAG头部,cmp存一个强连通分量的拓扑序. p ...

  6. 洛谷——P2341 [HAOI2006]受欢迎的牛//POJ2186:Popular Cows

    P2341 [HAOI2006]受欢迎的牛/POJ2186:Popular Cows 题目背景 本题测试数据已修复. 题目描述 每头奶牛都梦想成为牛棚里的明星.被所有奶牛喜欢的奶牛就是一头明星奶牛.所 ...

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

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

  8. poj2186 强连通缩点

    Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 29773   Accepted: 12080 De ...

  9. POJ2186 Popular Cows

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

随机推荐

  1. PHP开源论坛PunBB在IIS上部署和安装

    说明:对PHP的模式修饰符e来说:自 PHP 5.5.0 起废弃,而PunBB1.4.2还是有用到的(我曾用二者搭配,结果网站运行中会出现错误),所以我用了php-5.4.30. 一.运行环境 首先如 ...

  2. vmware 修改IP 提示子网掩码错误~

    我打开[编辑]->[虚拟网络编辑器]菜单 修改网络设置 提示 子网掩码错误 如下图所示 上网查询,使用如下方法解决问题 这个虚拟网络编辑器是给你添加网卡的,你添加vmnet1就是在你真实的电脑上 ...

  3. 几种常见 容器 比较和分析 hashmap, map, vector, list ...hash table

    list支持快速的插入和删除,但是查找费时; vector支持快速的查找,但是插入费时. map查找的时间复杂度是对数的,这几乎是最快的,hash也是对数的.  如果我自己写,我也会用二叉检索树,它在 ...

  4. 数据回复之TestDisk的使用

    1,选择[No Log]或者是[Create]进入 2.选择好要恢复的硬盘,回车 3.选择Intel或者其他的系统,大多数选择intel(windows)使用,回车确认 4.选择[Analyse](分 ...

  5. hrbrid需要做的

    1 返回并刷新 A webveiw push 到 B webview.当由B返回到A时候, A需要刷新页面.

  6. 监控数据库运行 - MS SQL 日常维护管理常用脚本(二)

    查看数据库登录名信息 use mastergoSELECT name AS LoginName , dbname AS DefaultDB , createdate AS CreateDate, up ...

  7. String类常用方法

    1.String类的特点,字符串一旦被初始化就不会被改变. 2.String对象定义的两种方式 ①String s = "affdf";这种定义方式是在字符串常量池中创建一个Str ...

  8. hashMap的get()方法,错用并发造成cpu和负载高

    一次线上问题的解决 线上发现服务cpu使用达到98%,负载高达200多,64核心cpu,下面介绍解决过程: 1.top命令查出占用cpu高的进程pid 2.使用jstack -l pid >du ...

  9. 新版本eclipse Neon 4.6.1,登录git报401 没有权限

    新版本eclipse Neon 4.6.1,登录git报401 没有权限 经过查找原因竟然是新的eclipse需要进行update.比较坑,新版eclipse竟然需要先更新一下才能用! eclipse ...

  10. 用vs2013(cpu-only)调试caffe的mnist

    在调试Mnist例子之前,首先需要用vs2013编译好caffe.详情请参见: [caffe-Windows]caffe+VS2013+Windows无GPU快速配置教程 按照上述教程编译好caffe ...