Poj 2186 Popular Cows
2016-05-15 14:30
447 查看
Popular Cows
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 29240 Accepted: 11831
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.
Source
USACO 2003 Fall
传送门
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 29240 Accepted: 11831
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.
Source
USACO 2003 Fall
传送门
/* Tarjan模板+缩点 */ #include<iostream> #include<cstdio> #include<cstring> #define MAXN 50001 using namespace std; struct data { int v,next; } e[MAXN]; int n,m,cut,belong[MAXN],head[MAXN],dfn[MAXN],stack[MAXN],low[MAXN],in[MAXN],top,tot; void init() { cut=tot=top=0; memset(head,-1,sizeof(head)); memset(stack,0,sizeof(stack)); memset(dfn,0,sizeof(dfn)); memset(low,0,sizeof(low)); memset(in,0,sizeof(in)); } void add_edge(int u,int v) { e[tot].v=v; e[tot].next=head[u]; head[u]=tot++; } void tarjan(int u) { int v; low[u]=dfn[u]=++cut; in[u]=1;//当前节点进栈 stack[top++]=u; for(int i=head[u];i!=-1;i=e[i].next)//遍历边 { v=e[i].v; if(!dfn[v])//未被访问 { tarjan(v); low[u]=min(low[u],low[v]); } else if(in[v]) { low[u]=min(low[u],dfn[v]); } } if(dfn[u]==low[u])//找到一个强联通分量的根 { tot++; do { v=stack[--top];//退栈 in[v]=0; belong[v]=tot;//tot为连通分量编号 } while(u!=v); } } int main() { while(scanf("%d %d",&n,&m)!=EOF) { int x,y; init(); for(int i=1;i<=m;i++) { cin>>x>>y; add_edge(x,y); } tot=0; for(int i=1;i<=n;i++) { if(!dfn[i]) tarjan(i); } int out[MAXN]={0},u,v; for(int i=1;i<=n;i++) { for(int j=head[i];j!=-1;j=e[j].next) { u=belong[i],v=belong[e[j].v]; if(u!=v) out[u]++; } } int flag=0; for(int i=1;i<=tot;i++) { if(!out[i])//找出度为0的连通块 { flag++; x=i; } } if(flag>1) printf("0\n"); else { int ans=0; for(int i=1;i<=n;i++) { if(belong[i]==x)//属于该连通块的点即为答案的贡献 ans++; } printf("%d\n",ans); } } return 0; }
相关文章推荐
- Linux:前期总结
- [架构设计]第一讲:什么是架构
- Linux发行版 CentOS6.5 修改默认主机名
- linux下编译jrtplib-3.9.1
- KNN Hadoop MapReduce
- linux协议栈中网卡相关的名词解释
- linux下超简单的ntp时间服务器
- Hadoop笔记
- LINUX---RPS/RFS转载
- linux企业常用服务---部署Nginx+Tomcat负载均衡集群
- Opentsdb设计之道
- Hadoop源码分析--MapReduce作业(job)提交源码跟踪
- Linux堆内存管理深入分析
- Nginx配置负载均衡
- powershell remote on computer
- ~手机APP:Termux --模拟Linux终端
- apache2.4.7虚拟机配置多域名(其中一个是二级域名)
- linux常用文本编辑器nano/vi/vim
- nginx 二级域名
- hadoop2.x常用端口、定义方法及默认端口、hadoop1.X端口对比