poj2186 Popular Cows 有向图 强连通分量
来源:互联网 发布:淘宝商城高跟鞋 编辑:程序博客网 时间:2024/05/09 01:14
Popular Cows
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 20718 Accepted: 8438
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.
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.
* 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 31 22 12 3
Sample Output
1
Hint
Cow 3 is the only cow of high popularity.
Source
USACO 2003 Fall
给个有向图,先求强连通分量,缩点之后,如果出度为0的只有一个,那个结果自然是这个缩点的点数,如果为0的大于1,则一定不连通,所以直接输出0就可以了!
#include <iostream>#include <stdio.h>#include <string.h>#include <stdlib.h>#define N 10510#define E 100000#define M 1000000using namespace std;int head[N],sta[M],num[N],re,ans,next[E],id[N],in[N],vec[E],vis[N],dfn[N],low[N],clock_m,edge_m;int addedge(int s,int e){ vec[edge_m]=e;next[edge_m]=head[s];head[s]=edge_m++;}int init(){ memset(vis,0,sizeof(vis)); memset(dfn,0,sizeof(dfn)); memset(low,0,sizeof(low)); memset(num,0,sizeof(num)); memset(head,-1,sizeof(head)); memset(id,0,sizeof(id)); memset(in,0,sizeof(in)); edge_m=0;clock_m=0;ans=0;re=0;}int tarjan(int x){ dfn[x]=low[x]=++clock_m; sta[++ans]=x; vis[x]=1; for(int i=head[x];i!=-1;i=next[i]){ int goal=vec[i]; if(!dfn[goal]){ tarjan(goal); low[x]=min(low[x],low[goal]); } else if(/*vis[goal]*/!id[goal]) low[x]=min(low[x],dfn[goal]); } if(low[x]==dfn[x]){ re++;int v; do{ v=sta[ans--];vis[v]=0;num[re]++;id[v]=re; }while(v!=x); } return 1;}int main(){ int tcase,n,m,s,e; while(scanf("%d%d",&n,&m)!=EOF) { //system("PAUSE"); init(); for(int i=0;i<m;i++){ scanf("%d%d",&s,&e); addedge(s,e); } for(int i=1;i<=n;i++) if(!dfn[i]) { tarjan(i); } for(int i=1;i<=n;i++){ for(int j=head[i];j!=-1;j=next[j]){ if(id[vec[j]]!=id[i]){ in[id[i]]++; } } } ans=0;int k=1; for(int i=1;i<=re;i++){ if(in[i]==0){ sta[ans++]=i;k=i; } } if(ans>1)printf("0\n"); else printf("%d\n",num[k]); } return 0;}
- poj2186 Popular Cows 有向图 强连通分量
- 【POJ2186】Popular Cows 有向图强连通分量详解(再水一篇怕被骂、、所以。。)
- 有向图强连通分量-poj-2186-Popular Cows
- POJ2186 Popular Cows【Kosaraju】【强连通分量】
- POJ2186 Popular Cows【Tarjan】【强连通分量】
- poj2186 Popular Cows(强连通分量tarjan)
- POJ2186 Popular Cows 强连通分量
- POJ2186 Popular Cows(强连通分量)
- POJ2186 Popular Cows 强连通分量
- POJ2186 Popular Cows 【图论】【强连通分量】
- poj2186 Popular Cows(强连通分量)
- POJ2186 Popular Cows 【强连通分量】+【Kosaraju】+【Tarjan】+【Garbow】
- poj2186 Popular Cows(强连通分量)(korasaju||tarjan模板题)
- poj2186 Popular Cows--Kosaraju算法 & 缩点 & 强连通分量
- POJ2186 Popular Cows 【强连通分量】+【Kosaraju】+【Tarjan】
- 图论:POJ2186-Popular Cows (求强连通分量)
- poj2186 Popular Cows --- 强连通
- poj2186 Popular Cows【强连通】
- java基本数据类型详细讲解。
- java获取指定日期的时间戳
- 命令模式解析
- 33. 浮点数不是真实的
- MFC多进程多线程编程之--多线程
- poj2186 Popular Cows 有向图 强连通分量
- 2013年9月2日星期一(8-4,平移和旋转)
- deferred initcalls与模块化
- 安卓开发学习笔记
- 2013年9月5日星期四(8-5,缩放)
- c/c++判断文件是否存在
- flash memory缺陷与改进
- MYSQL 常用命令列表
- Ubuntu下 ssh : connect to host localhost port 22:Connection refused