Popular Cows
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 25945   Accepted: 10612

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.

题意是给你几头牛(......),然后给你一个A牛喜欢B牛 这类的关系,这种喜欢的关系还可以传递,比如A喜欢B,B喜欢C,那就A也喜欢C了。问有多少头牛被所有牛喜欢。

Tarjan模板题,话说看懂了Tarjan之后还是很爽的。

用Tarjan算法缩点之后,要出度为0的点只有一个才满足要求,想象一下,要是两个的话,就不会有牛被所有其他牛喜欢的。

然后看出度为0的点内又有多少个点即可。

代码:

#include <iostream>
#include <string>
#include <cstring>
#include <queue>
#pragma warning(disable:4996)
using namespace std; int head[10005],LOW[10005],DFN[10005],instack[10005],Stack[10005],Belong[10005],out[10005];
int n,m,edge_num,Dindex,Stop,Bcnt; struct edge{
int to;
int next;
}Edge[50005]; void init()
{
edge_num=0;
Stop=Bcnt=Dindex=0; memset(Edge,-1,sizeof(Edge));
memset(head,-1,sizeof(head));
memset(LOW,0,sizeof(LOW));
memset(DFN,0,sizeof(DFN));
memset(instack,0,sizeof(instack));
memset(Stack,0,sizeof(Stack));
memset(Belong,0,sizeof(Belong));
memset(out,0,sizeof(out));
} void addedge(int u,int v)
{
Edge[edge_num].to=v;
Edge[edge_num].next=head[u];
head[u]=edge_num;
edge_num++;
} void tarjan(int i)
{
int j;
DFN[i]=LOW[i]=++Dindex;
instack[i]=true;
Stack[++Stop]=i; for(j=head[i];j!=-1;j=Edge[j].next)
{
int v=Edge[j].to;
if(DFN[v]==0)
{
tarjan(v);
LOW[i]=min(LOW[i],LOW[v]);
}
else if(instack[v]==1)
{
LOW[i]=min(LOW[i],DFN[v]);
}
} if(DFN[i]==LOW[i])
{
Bcnt++;
do
{
j=Stack[Stop--];
instack[j]=false;
Belong[j]=Bcnt;
}
while(j!=i);
}
} void solve()
{
int i,j,u,v;
init(); cin>>n>>m;
for(i=1;i<=m;i++)
{
cin>>u>>v;
addedge(u,v);
} for(i=1;i<=n;i++)
{
if(!DFN[i])
tarjan(i);
}
for(i=1;i<=n;i++)
{
for(j=head[i];j!=-1;j=Edge[j].next)
{
if(Belong[i]!=Belong[Edge[j].to])
out[Belong[i]]++;//计算缩点后每个点的出度
}
}
int out_num=0,import;
for(i=1;i<=Bcnt;i++)
{
if(!out[i])
{
out_num++;
import=i;
}
}
int temp=0;
if(out_num==1)
{
for(i=1;i<=n;i++)
{
if(Belong[i]==import)
{
temp++;
}
}
cout<<temp<<endl;
}
else
{
cout<<0<<endl;
} } int main()
{
//freopen("i.txt","r",stdin);
//freopen("o.txt","w",stdout); solve();
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 2186:Popular Cows Tarjan模板题的更多相关文章

  1. POJ - 2186  Popular Cows tarjain模板题

    http://poj.org/problem?id=2186 首先求出所有的强连通分量,分好块.然后对于每一个强连通分量,都标记下他们的出度.那么只有出度是0 的块才有可能是答案,为什么呢?因为既然你 ...

  2. poj 2186 Popular Cows tarjan

    Popular Cows Description Every cow's dream is to become the most popular cow in the herd. In a herd ...

  3. [poj 2186]Popular Cows[Tarjan强连通分量]

    题意: 有一群牛, a会认为b很帅, 且这种认为是传递的. 问有多少头牛被其他所有牛认为很帅~ 思路: 关键就是分析出缩点之后的有向树只能有一个叶子节点(出度为0). 做法就是Tarjan之后缩点统计 ...

  4. POJ 2186 Popular Cows tarjan缩点算法

    题意:给出一个有向图代表牛和牛喜欢的关系,且喜欢关系具有传递性,求出能被所有牛喜欢的牛的总数(除了它自己以外的牛,或者它很自恋). 思路:这个的难处在于这是一个有环的图,对此我们可以使用tarjan算 ...

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

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

  6. tarjan缩点练习 洛谷P3387 【模板】缩点+poj 2186 Popular Cows

    缩点练习 洛谷 P3387 [模板]缩点 缩点 解题思路: 都说是模板了...先缩点把有环图转换成DAG 然后拓扑排序即可 #include <bits/stdc++.h> using n ...

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

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

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

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

  9. POJ 2186 Popular Cows (强联通)

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

随机推荐

  1. dubbo 相关面试题 有用(转)

    调用关系说明: · 0. 服务容器负责启动,加载,运行服务提供者. · 1. 服务提供者在启动时,向注册中心注册自己提供的服务. · 2. 服务消费者在启动时,向注册中心订阅自己所需的服务. · 3. ...

  2. ubuntu 新建用户后 不能使用TAB键、上下键,命令行不显示当前路径的解决

    因默认ubuntu创建的普通帐号,默认shell为/bin/sh,而这不支持tab等键的,所以将「指定用户」帐号的shell改为/bin/bash就可以了. 1.查看当前的shell:# echo $ ...

  3. PHP中strlen和mb_strlen函数的使用方式的不同

    (1)strlen  获取字符串长度 (2)mb_strlen  使用方法:   int mb_strlen ( string $str [, string $encoding ] )返回给定的字符串 ...

  4. 010.Oracle数据库 , ORDER BY 按升序降序排序

    /*Oracle数据库查询日期在两者之间*/ SELECT DISTINCT ATA FROM LM_FAULT WHERE ( OCCUR_DATE BETWEEN to_date( '2017-0 ...

  5. mysql concat与concat_ws区别

    select concat('大','小') as size from 表 查询出结果为:大小 select concat('大',NULL) as size from 表 查询出结果为:null c ...

  6. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-chevron-right

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...

  7. POJ 3321:Apple Tree 树状数组

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 22131   Accepted: 6715 Descr ...

  8. golang 读取 chrome保存的网站账号信息

    package main import ( "database/sql" "fmt" "log" "os" " ...

  9. Python 中使用 ddt 来进行数据驱动,批量执行用例,修改ddt代码

    1. 什么是数据驱动? 使用数据驱动有什么好处? 用例执行是靠数据来驱动的,每条测试用例除了测试数据不一样意外,所有的用例代码都是一样的,为了使用例批量执行,我们会使用数据驱动的思想来批量执行测试用例 ...

  10. SQL 、LINQ日前比较

      using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; ...