Popular Cows+求强联通量简单题+tarjan算法+POJ
来源:互联网 发布:编程达人全套视频 编辑:程序博客网 时间:2024/05/22 12:26
Popular Cows
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 23066 Accepted: 9451
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
解决方案:先求强连通分量,缩点,反向建图,求入度为0的点。
code:
#include<iostream>#include<cstdio>#include<cstring>#include<vector>#include<stack>#define MMAX 10003using namespace std;int low[MMAX],dfn[MMAX],instack[MMAX];int in[MMAX];int fa[MMAX],cnt[MMAX];int index;stack<int>S;vector<int>Map[MMAX];int N,M;void init(){ index=0; memset(cnt,0,sizeof(cnt)); memset(instack,0,sizeof(instack)); memset(in,0,sizeof(in)); for(int i=0; i<=N; i++) { fa[i]=i; Map[i].clear(); } while(!S.empty()) S.pop();}void tarjan(int u){ low[u]=dfn[u]=index++;///时间戳 S.push(u); instack[u]=1; int len=Map[u].size(); for(int i=0; i<len; i++) { int v=Map[u][i]; if(!instack[v]) { tarjan(v); low[u]=min(low[u],low[v]); } else if(instack[v]==1) { low[u]=min(low[v],low[u]); } } if(dfn[u]==low[u]) { while(!S.empty()) { int temp=S.top(); S.pop(); fa[temp]=u; instack[temp]=2; if(temp==u) break; } }}int main(){ while(~scanf("%d%d",&N,&M)) { init(); for(int i=0; i<M; i++) { int a,b; scanf("%d%d",&a,&b); Map[a].push_back(b); } for(int i=1; i<=N; i++) { if(!instack[i]) { tarjan(i); } } for(int i=1; i<=N; i++) { int len=Map[i].size(); for(int j=0; j<len; j++) { int v=Map[i][j]; if(fa[v]!=fa[i]) { in[fa[i]]++; } } } int _sum=0,ss=0; int pre=-1; for(int i=1; i<=N; i++) { if(!in[fa[i]]) { if(pre!=-1&&pre!=fa[i]) ss++; _sum++; pre=fa[i]; } } if(!ss) printf("%d\n",_sum); else printf("0\n"); } return 0;}
0 0
- Popular Cows+求强联通量简单题+tarjan算法+POJ
- poj 2186 Popular Cows 强联通分量tarjan/Kosaraju
- POJ 2186 Popular Cows(强联通分量缩点+tarjan算法)
- POJ 2186 Popular Cows Kosaraju求强联通分量
- POJ 2186 Popular Cows(tarjan求强连通)
- POJ 2186 Popular Cows(Tarjan算法求强连通分量)
- POJ 2186 Popular Cows(强连通分量Tarjan算法)
- POJ 2186 Popular Cows (强联通)
- POJ 2186 Popular Cows (强联通分量)
- POJ 2186:Popular Cows(tarjan算法入门题)
- POJ 2186 Popular Cows Tarjan算法
- poj-2186-Popular Cows (tarjan算法)
- poj2186 Popular Cows 强联通
- POJ 2186 popular cow 有向图的强联通问题 Tarjan算法
- POJ 2186 popular cow 有向图的强联通问题 Tarjan算法
- POJ 2186 Popular Cows(强连通分量缩点,Tarjan算法)
- poj 2186 Popular Cows (强联通分量+缩点)
- POJ 2186 Popular Cows(强联通分量)
- jobdu1475 非常可乐
- “Spark上流式机器学习算法实现”中期检查报告
- 使用百度地图API根据坐标显示地图
- uva133 The Dole Queue ( 约瑟夫环的模拟)
- dedecms 标题和关键词的调用
- Popular Cows+求强联通量简单题+tarjan算法+POJ
- 参与unity非游戏行业开发者大会小结
- 给 C# 开发者的代码审查清单
- hdoj 2277 Change the ball 【找规律】
- CSS 参考手册
- 利用xib自定义view,在uicontrollerview中使用
- Linux命令行之逗趣无极限
- 中期报告
- 构造java实例的时候各个语句调用的顺序