poj2186Popular Cows【scc+缩点】
来源:互联网 发布:中国网络女作家排行榜 编辑:程序博客网 时间:2024/04/25 08:41
Language:
Popular Cows
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 27987 Accepted: 11275
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
题意:没头牛都有认为一头牛最知名具有传递性求被所有牛都认为知名的牛的个数
解题思路:scc+缩点如果为强连通图数目为n缩点后出度大于1则不存在;出度为一时输出出度为1的scc的牛数即可
#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>#include<cmath>#include<stack>#include<vector>#define inf 0x3f3f3f3f3fusing namespace std;const int maxn=100010;int dfs_clock,scc_cnt;int head[maxn];bool instack[maxn];int low[maxn],dfn[maxn];int sccno[maxn];int in[maxn],out[maxn];stack<int>S;vector<int>G[maxn];vector<int>scc[maxn];struct Node{int from,to,next;}A[maxn];void init(){dfs_clock=scc_cnt=0;memset(head,-1,sizeof(head));memset(low,0,sizeof(low));memset(dfn,0,sizeof(dfn));memset(in,0,sizeof(in));memset(out,0,sizeof(out));memset(sccno,0,sizeof(sccno));memset(instack,false,sizeof(instack));}int MIN(int a,int b){return a<b?a:b;}void tarjan(int u,int pre){low[u]=dfn[u]=++dfs_clock;S.push(u);instack[u]=true;int v;for(int k=head[u];k!=-1;k=A[k].next){v=A[k].to;if(!dfn[v]){tarjan(v,u);low[u]=MIN(low[u],low[v]);}else if(instack[v]){low[u]=MIN(low[u],dfn[v]);}}if(low[u]==dfn[u]){scc_cnt++;G[scc_cnt].clear();scc[scc_cnt].clear();while(1){v=S.top();S.pop();instack[v]=false;sccno[v]=scc_cnt;scc[scc_cnt].push_back(v);if(u==v)break;}}}void suodian(int m){for(int i=0;i<m;++i){int u=sccno[A[i].from];int v=sccno[A[i].to];if(u!=v){G[u].push_back(v);in[v]++;out[u]++;}}}int main(){int n,m,i,j,k;while(scanf("%d%d",&n,&m)!=EOF){init();for(i=0;i<m;++i){scanf("%d%d",&A[i].from,&A[i].to);A[i].next=head[A[i].from];head[A[i].from]=i;}for(i=1;i<=n;++i){if(!dfn[i])tarjan(i,-1);}suodian(m);int ans=0,pos;for(i=1;i<=scc_cnt;++i){if(!out[i]){ans++;pos=i;}}if(scc_cnt==1){printf("%d\n",n);}else if(ans>1){printf("%d\n",0);}else printf("%d\n",scc[pos].size());}return 0;}
0 0
- poj2186Popular Cows【scc+缩点】
- poj2186Popular Cows tarjan缩点
- poj2186Popular Cows(强连通+缩点)
- POJ2186Popular Cows(强连通分量+缩点)
- poj 2186 Popular Cows(SCC缩点)
- poj2186Popular Cows
- POJ2186Popular Cows
- poj2186Popular Cows
- poj--2186--Popular Cows (scc+缩点)
- scc缩点
- POJ2186Popular Cows(强连通分量)
- hdoj1827Summer Holiday【scc+缩点】
- hdoj3836Equivalent Sets【scc +缩点】
- hdoj2767Proving Equivalences【scc+缩点】
- poj2186Popular Cows_ 强连通分支_缩点tarjan算法
- poj 2186 Popular Cows 【有向图求SCC +缩点】【求图中有多少个点 可以由其余所有点通过存在路径到达】
- poj2186Popular Cows【强连通】【debug两小时】
- poj2186Popular Cows【强连通】【debug两小时】
- block使用的一个例子
- android 虚拟摇杆绘制
- iOS post请求 (普通请求)
- JSP内置对象
- 深入理解java语言的class文件格式(三)
- poj2186Popular Cows【scc+缩点】
- 过去中国创业者们最担心的是中国最大的三家互联网公司
- Eclipse如何集成MySQL数据库
- 【有感】——工业4.0来了,我们还能做点什么?
- 【linux】关于中文输入法设置
- Oracle到SAP HANA实时复制系列:Replication Server的安装与配置
- 通过VS设置使程序具有管理员权限
- 深入理解java语言的class文件格式(四)
- MFC实现多个界面跳转