poj 2186 Popular Cows
2016-07-11 13:37
260 查看
Popular Cows
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
USACO 2003 Fall
tarjan一直没学会,今天写了道入门题。主要思路是用tarjan()求出强连通分量,记录每一个强连通分量的出度,出度为0的就是大家都认可的popular cow。在数组问题上卡了很久,后来发现u[i],v[i]要开5*10100才ac。
15701878
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 29908 | Accepted: 12131 |
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
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*5],v[M*5],first[M*5],mnext[M*5],ans[M],outdu[M]; int dfs_clock,scc_cnt,stack_count,my_stack[M*5],pre[M],sccno[M],lowlink[M]; void Init() { memset(first,-1,sizeof(first)); memset(mnext,0,sizeof(mnext)); memset(my_stack,0,sizeof(my_stack)); memset(lowlink,0,sizeof(lowlink)); memset(sccno,0,sizeof(sccno)); memset(pre,0,sizeof(pre)); memset(ans,0,sizeof(ans)); memset(outdu,0,sizeof(outdu)); } void Link() { for(int i=1;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!=-1;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=0; 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=0; for(int i=1;i<=n;i++) if(!pre[i])dfs(i); } int main() { while(~scanf("%d%d",&n,&m)){ Init(); Link(); Tarjan(); if(scc_cnt==1){ printf("%d\n",n); continue; } for(int i=1;i<=n;i++){ for(int j=first[i];j!=-1;j=mnext[j]){ int vx=v[j]; if(sccno[i]!=sccno[vx]) outdu[sccno[i]]++; } } int res=0; for(int i=1;i<=scc_cnt;i++) if(!outdu[i]){ if(!res)res=ans[i]; else {res=0;break;} } printf("%d\n",res); } return 0; }
相关文章推荐
- 虚拟机CentOS静态IP
- 基础级 - Linux使用积累
- solr5.5 部署TOMCAT
- WEB服务器:Apache、Tomcat、JBoss、WebLogic的区别与关系
- Mac OS上安装Tomcat服务器的简单步骤
- popupWindow弹出自定义布局+弹出布局的位置控制
- Java后端WebSocket的Tomcat实现
- linux(centos)搭建SVN服务器
- Windows主机里利用VMware安装Linux(CentOS)虚拟机,Host-only连接上网方式详解
- CentOS Linux系统下更改Apache默认网站目录
- Linux下tar.xz tar.bz2 tar.gz 等文件的解压
- apache commons fileupload JIRA报告(2016-05-27更新)
- Hadoop 2.2.0安装和配置lzo
- OpenMp入门
- apache commons fileupload 可靠性报告(2016-07-01更新)
- Elasticsearch 2.3.3 插件和集群 centos 7
- linux下安装nginx详细教程
- Linux下三行指令将Tomcat配置成服务及自启动
- 常用网站
- Linux系统PWM驱动