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

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


Sample Output


Hint

Cow 3 is the only cow of high popularity.

Source

题解

tarjan一直没学会,今天写了道入门题。主要思路是用tarjan()求出强连通分量,记录每一个强连通分量的出度,出度为0的就是大家都认可的popular cow。在数组问题上卡了很久,后来发现u[i],v[i]要开5*10100才ac。

15701878

  ksq2013 2186 Accepted 1304K 94MS C++ 1969B 2016-07-11 13:36:28
#include<cstdio>
#include<cstring>
#include<iostream>
#define M 10100
using namespace std;
int n,m,u[M*],v[M*],first[M*],mnext[M*],ans[M],outdu[M];
int dfs_clock,scc_cnt,stack_count,my_stack[M*],pre[M],sccno[M],lowlink[M];
void Init()
{
memset(first,-,sizeof(first));
memset(mnext,,sizeof(mnext));
memset(my_stack,,sizeof(my_stack));
memset(lowlink,,sizeof(lowlink));
memset(sccno,,sizeof(sccno));
memset(pre,,sizeof(pre));
memset(ans,,sizeof(ans));
memset(outdu,,sizeof(outdu));
}
void Link()
{
for(int i=;i<=m;i++){
scanf("%d%d",&u[i],&v[i]);
mnext[i]=first[u[i]];
first[u[i]]=i;
}
}
void dfs(int ux)
{
pre[ux]=lowlink[ux]=++dfs_clock;
my_stack[++stack_count]=ux;
int vx;
for(int i=first[ux];i!=-;i=mnext[i]){
vx=v[i];
if(!pre[vx]){
dfs(vx);
lowlink[ux]=min(lowlink[ux],lowlink[vx]);
}
else if(!sccno[vx])
lowlink[ux]=min(lowlink[ux],pre[vx]);
}
if(lowlink[ux]==pre[ux]){
scc_cnt++;
int tmp=;
do{
tmp++;
vx=my_stack[stack_count--];
sccno[vx]=scc_cnt;
}while(vx!=ux);
ans[scc_cnt]=tmp;
}
}
void Tarjan()
{
dfs_clock=scc_cnt=stack_count=;
for(int i=;i<=n;i++)
if(!pre[i])dfs(i);
}
int main()
{
while(~scanf("%d%d",&n,&m)){
Init();
Link();
Tarjan();
if(scc_cnt==){
printf("%d\n",n);
continue;
}
for(int i=;i<=n;i++){
for(int j=first[i];j!=-;j=mnext[j]){
int vx=v[j];
if(sccno[i]!=sccno[vx])
outdu[sccno[i]]++;
}
}
int res=;
for(int i=;i<=scc_cnt;i++)
if(!outdu[i]){
if(!res)res=ans[i];
else {res=;break;}
}
printf("%d\n",res);
}
return ;
}

poj 2186 Popular Cows的更多相关文章

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

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

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

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

  3. POJ 2186 Popular Cows (强联通)

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

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

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

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

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

  6. POJ 2186 Popular Cows(Targin缩点)

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

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

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

  8. POJ 2186 Popular Cows(强连通)

                                                                  Popular Cows Time Limit: 2000MS   Memo ...

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

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

随机推荐

  1. sharepoint2013- Office web app server2013详细的安装和部署

    前提条件 Office web app server2013不能跟sharepoint server2013安装在同一台服务器上,如果安装在同一台服务器上将提示如下错误: 后来查询资料:  按照官方文 ...

  2. mac安装Aws cli失败

    OS X EI 10.11 报错信息如下: Found existing installation: six 1.4.1 DEPRECATION: Uninstalling a distutils i ...

  3. 用(*.frm *.MYD *.MYI)文件恢复MySql数据库

    保存下来以防以后遇到 今天还原mysql数据库时,看到那个data文件夹下好几个文件,还没有.sql文件,没有见过,总结下.Data文件夹里面包括:数据库名文件夹,文件夹里包括,*.frm,*.MYI ...

  4. Android Activity动画

    动画XML文件 slide_right_in.xml <?xml version="1.0" encoding="utf-8"?> <set ...

  5. view渐变色,透明度渐变

    1 功能描述 开发中经常遇到这样的需求:view2显示在view1上面,透过view2可以渐渐的看到view1.效果如图1所示:view1是一个imageView,view2是一个普通view.vie ...

  6. Android源码分析之SharedPreferences

    在Android的日常开发中,相信大家都用过SharedPreferences来保存用户的某些settings值.Shared Preferences 以键值对的形式存储私有的原生类型数据,这里的私有 ...

  7. Android系统性能调优工具介绍

    http://blog.csdn.net/innost/article/details/9008691 经作者授权,发表Tieto某青年牛的一篇<程序员>大作. Android系统性能调优 ...

  8. OSGI入门笔记

    OSGI框架为Java定义了一个动态模块化系统,它使你可以更好地控制代码结构,动态管理代码的生命周期,并且提供了代码写作的松耦合方式:更值得称道的是,它的规范文档描述详尽.--<OSGI实战&g ...

  9. .Net中使用aliases让相同命名空间的dll引用共存

    有些不得已的时候,我们需要同时在代码中使用某个dll的不同版本.比如用低版本的dll中的方法导出数据,然后使用高版本的方法导入数据来实现数据的升级. 又或者需要同时使用第三方的dll不同版本.如何使它 ...

  10. iOS 导航栏实现总结

    目标: 在UI界面中实现 整体效果的导航栏, 比如1 首页无导航条,次页有导航条, 2 导航条中不包含下方不包含黑边 3 导航条包含多个筛选项 等等 问题: 用系统带的NavigateBar 来实现时 ...