poj 2186 Popular Cows(强连通分量模板题,tarjan)
来源:互联网 发布:linux route指令 编辑:程序博客网 时间:2024/05/20 07:54
Popular Cows
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 30374 Accepted: 12323
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题意:有n头牛和m个关系,m个关系(a,b)表示a觉得b很受欢迎,并且如果a觉得b很受欢迎,b觉得c很受欢迎,那么a觉得c很受欢迎,现在求出受所有牛(不包括自身)欢迎的牛的个数
思路:刚开始还以为要用并查集去处理,其实并不用。
这里我们把所有的牛进行缩点,这样每个点里的牛都是相互之间受欢迎的,因为受所有牛欢迎的牛相互之间肯定是有关系的,不然就不可能受“所有”牛欢迎了
并且缩完点之后是一个DAG图, 有向无环图。(如果两个点有环,那么会缩成一个点)
如果出度为0的点只有一个那么输出这个点包含的牛的个数即可,不然输出0
因为如果出度大于0的点不止一个那么图一定不互相连通(有一些牛跟这个点没任何关系)
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>using namespace std;#define N 10050#define M 50050struct Edge{ int v,next;} edge[M];int cnt,head[N];int dfn[N];int low[N];int s[N],top;int instack[N];int num;int index;int belong[N];int out[N];void init(){ cnt=0; num=1; index = 0; top=0; memset(dfn, -1, sizeof(dfn)); memset(low, -1, sizeof(low)); memset(head, -1, sizeof(head)); memset(instack, 0, sizeof(instack)); memset(belong, 0, sizeof(belong)); memset(out, 0, sizeof(out));}void addedge(int u,int v){ edge[cnt].v=v; edge[cnt].next=head[u]; head[u]=cnt++;}void tarjan(int u){ int v, i; dfn[u] = low[u] = num++; instack[u] = 1; s[++top] = u; for(i = head[u]; i != -1; i = edge[i].next) { v = edge[i].v; if(dfn[v] == -1) { tarjan(v); low[u] = min(low[u], low[v]); } else if(instack[v]) { low[u] = min(low[u], dfn[v]); } } if(low[u] == dfn[u]) { index++; do { v = s[top--]; instack[v] = 0; belong[v] = index; } while(top != 0 && v != u); }}int main(){ int n,m; int u,v; while(~scanf("%d %d",&n,&m)) { init(); for(int i=1; i<=m; i++) { scanf("%d %d",&u,&v); addedge(u,v); } for(int i=1; i<=n; i++) if(dfn[i]==-1) tarjan(i); for(int j=1; j<=n; j++) for(int i=head[j]; i!=-1; i=edge[i].next) if(belong[j]!=belong[edge[i].v]) out[belong[j]]++; int ans,tot=0; for(int i=1; i<=index; i++) if(!out[i]) { tot++; ans=i; } for(int i=1;i<=n;i++) if(!belong[i]) tot=0; if(tot==1) { int q=0; for(int i=1; i<=n; i++) if(belong[i]==ans) q++; printf("%d\n",q); } else printf("0\n"); } return 0;}
0 0
- poj 2186 Popular Cows(强连通分量模板题,tarjan)
- POJ 2186 Popular Cows(强连通分量模板题)
- POJ 2186 Popular Cows(强连通分量Tarjan算法)
- poj2186 Popular Cows(强连通分量)(korasaju||tarjan模板题)
- poj 2186 Popular Cows (强连通分量 tarjan)
- POJ 2186 Popular Cows 强连通分量 Kosaraju or tarjan
- [poj 2186]Popular Cows[Tarjan强连通分量]
- POJ 2186 Popular Cows tarjan缩点 强连通分量
- poj 2186 Popular Cows (Tarjan 强连通分量+压缩点)
- poj 2186 Popular Cows(tarjan + 强连通分量 + 缩点)
- POJ 2186 Popular Cows(Tarjan算法求强连通分量)
- POJ 2186 Popular Cows(强连通分量缩点,Tarjan算法)
- poj 2186 Popular Cows(强连通分量,tarjan或Kosaraju)
- poj 2186 Popular Cows(Tarjan,强连通分量缩点)
- POJ 2186 Popular Cows(Tarjan + 强连通分量缩点)
- POJ 2186-Popular Cows ---强连通分量
- POJ 2186 Popular Cows / 强连通分量
- POJ 2186 Popular Cows 强连通分量
- 配置javaagent
- Kth Smallest Element in a Sorted Matrix
- TCP Pacing的linux内核代码
- 一道程序初始化顺序题目引起的讨论
- Jquery当前日期的前一天
- poj 2186 Popular Cows(强连通分量模板题,tarjan)
- LeetCode105. Construct Binary Tree from Preorder and Inorder Traversal
- Spring之IOC
- WMS系统对接OMS接口经验
- commons-lang3-3.1.jar在BuildPath导入后运行仍报错:java.lang.ClassNotFoundException: org.apache.commons.lang3.S
- C语言静态分配数组和动态分配数组
- DMA
- java中break
- 每日一linux命令(44)-------netstat命令