Popular Cows
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 39115   Accepted: 15937

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.
 
Tarjan入门题,果然有的算法入门题就需要应用啊。。
求强连通分量的板子差不多找到个舒服的了,为了速度起见还是不用STL了吧。
这个题,问有没有点能被其他所有的点到达。强连通分量中是各自满足条件的
由于有向无环图中,出度为零的点是满足这个条件的。所以我们可以使用tarjan来找到所有的强连通分量,并进行缩点,对强连通分量进行编号,把每个点标记一下所属于的强连通分量,然后把图遍历一下,求出每个强连通分量的出度。
最后判断出度为0的强连通分量个数,若为1个,则存在,输出编号为这个的点个数即可。否则不存在。
   #include<cstdio>
#include<cstring>
#include<iostream>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<vector>
#include<stack>
//#include<bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define ll long long
#define inf 1000000000
#define maxn 10005
#define maxm 100005
#define eps 1e-10
#define for0(i,n) for(int i=1;i<=(n);++i)
#define for1(i,n) for(int i=1;i<=(n);++i)
#define for2(i,x,y) for(int i=(x);i<=(y);++i)
#define for3(i,x,y) for(int i=(x);i>=(y);--i)
#define mod 1000000007
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>'') {if(ch=='-') f=-;ch=getchar();}
while(ch>=''&&ch<='') {x=*x+ch-'';ch=getchar();}
return x*f;
}
struct node{
int to,next;
}edge[maxm];
int n,m,head[maxn],vis[maxn],dfn[maxn],low[maxn],du[maxn],num[maxn],cnt,timi,stack1[maxn],top,cut;
void init()
{
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
memset(head,-,sizeof(head));
memset(vis,,sizeof(vis));
memset(num,,sizeof(num));
memset(du,,sizeof(du));
cnt=;
timi=;
top=;
cut=;
}
stack <int> s;
void addedge(int u,int v)
{
edge[cnt].to=v;
edge[cnt].next=head[u];
head[u]=cnt;
cnt++;
}
void tarjan(int u)
{
dfn[u]=timi;
low[u]=timi;
timi++;
s.push(u);
//stack1[top]=u;top++;
vis[u]=;
for(int i=head[u];i!=-;i=edge[i].next)
{
int v=edge[i].to;
if(!dfn[v]) {
tarjan(v);
low[u]=min(low[u],low[v]);
}
else{
low[u]=min(low[u],dfn[v]);
}
}
if(low[u]==dfn[u])
{
cut++;
int x=s.top();
while(!s.empty()&&x!=u)
{
vis[s.top()]=;
//vis[stack1[top]]=2;
num[s.top()]=cut;
s.pop();
x=s.top();
}
num[s.top()]=cut;
s.pop();
}
}
int main()
{
int a,b;
while(~scanf("%d%d",&n,&m))
{
init();
for(int i=;i<=m;++i)
{scanf("%d%d",&a,&b);
addedge(a,b);
}
for(int i=;i<=n;++i)
{
if(!dfn[i]) tarjan(i);
}
for(int i=;i<=n;++i)
{
for(int j=head[i];j!=-;j=edge[j].next)
{
if(num[i]!=num[edge[j].to]) {du[num[i]]++;
}
}
}
int sum=,x;
for(int i=;i<=cut;++i)
{
if(!du[i]) {sum++;x=i;}
}
if(sum==)
{
sum=;
for(int i=;i<=n;++i)
if(num[i]==x) sum++;
printf("%d\n",sum);
}
else puts("0\n");
}
return ;
}

POJ 2168 Popular cows [Tarjan 缩点]的更多相关文章

  1. POJ 2186 Popular Cows tarjan缩点算法

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

  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(SCC 缩点)

    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[Tarjan强连通分量]

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

  5. USACO 2003 Fall Orange Popular Cows /// tarjan缩点 oj22833

    题目大意: n头牛,m个崇拜关系,并且崇拜具有传递性 如果a崇拜b,b崇拜c,则a崇拜c 求最后有几头牛被所有牛崇拜 强连通分量内任意两点都能互达 所以只要强联通分量内有一点是 那么其它点也都会是 按 ...

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

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

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

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

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

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

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

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

随机推荐

  1. git出现误修改如何撤销

    场景1:当你改乱了工作区某个文件的内容,想直接丢弃工作区的修改时,用命令git checkout -- file. 场景2:当你不但改乱了工作区某个文件的内容,还添加到了暂存区时,想丢弃修改,分两步, ...

  2. mysql 存储过程 例子

    DROP PROCEDURE IF EXISTS variable_demo; delimiter // CREATE PROCEDURE variable_demo() BEGIN select ' ...

  3. Flask初学者:session操作

    cookie:是一种保存数据的格式,也可以看成是保存数据的一个“盒子”,服务器返回cookie给浏览器(由服务器产生),由浏览器保存在本地,下次再访问此服务器时浏览器就会自动将此cookie一起发送给 ...

  4. 江西理工大学编程俱乐部 2328 Star

    : Star 时间限制: C/C++ s Java/Python s 内存限制: MB 答案正确: 提交: 题目描述 31世纪,人类世界的科技已经发展到了空前的高度,星际移民,星际旅游早已经不再是问题 ...

  5. Flume-NG源码分析-整体结构及配置载入分析

    在 http://flume.apache.org 上下载flume-1.6.0版本,将源码导入到Idea开发工具后如下图所示: 一.主要模块说明 flume-ng-channels 里面包含了fil ...

  6. 【Kubernetes】资源列表

    1.Kubernetes资源列表 https://www.cnblogs.com/linuxk/p/10436260.html

  7. 5、CSS基础part-3

    1.CSS列表 ①类型 ul.disc {list-style-type: disc} ②位置 ul.inside {list-style-position: inside} ③列表图像 2.表格

  8. win7装python3.6提示api-ms-win-runtime-1-1-0.dll丢失

    win7为MSDN下的旗舰版,没有servicepack1那个,刚开始安装python3.6提示必须得安装servicepack1,于是乎到微软官网下了个900mb大小的安装包. https://ww ...

  9. jqgrid postData setGridParam 调用多次时查询条件累加的问题

    var postData = $(gridId).jqGrid("getGridParam", "postData"); $.each(paras, funct ...

  10. [DM8168]Linux下控制GPIO实现LED流水灯

    首先加载驱动模块,应用程序通过调用API实现GPIO控制功能. 驱动程序: /* * fileName: led_gpio.c * just for LED GPIO test * GP1_14 -& ...