POJ 2186 Tarjan 缩点 解题报告
来源:互联网 发布:分水岭算法代码 编辑:程序博客网 时间:2024/04/29 18:39
Popular Cows
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
【解题报告】
题意:有n头牛和m个关系,m个关系(a,b)表示a觉得b很受欢迎,并且如果a觉得b很受欢迎,b觉得c很受欢迎,那么a觉得c很受欢迎,现在求出受所有牛(不包括自身)欢迎的牛的个数
思路:刚开始还以为要用并查集去处理,其实并不用。
这里我们把所有的牛进行缩点,这样每个点里的牛都是相互之间受欢迎的,因为受所有牛欢迎的牛相互之间肯定是有关系的,不然就不可能受“所有”牛欢迎了
并且缩完点之后是一个DAG图, 有向无环图。(如果两个点有环,那么会缩成一个点)
如果出度为0的点只有一个那么输出这个点包含的牛的个数即可,不然输出0
因为如果出度大于0的点不止一个那么图一定不互相连通(有一些牛跟这个点没任何关系)
代码如下:
#include<cstdio>#include<cstring>#include<algorithm> using namespace std;#define N 10050 #define M 50050 struct Edge{int v,next;}edge[M]; int cnt,head[N]; int dfn[N],low[N],s[N],top,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; while(scanf("%d %d",&n,&m)!=EOF) { init(); for(int i=1;i<=m;i++) { int u,v; 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; }
- POJ 2186 Tarjan 缩点 解题报告
- POJ 1144 Tarjan 割点 解题报告
- HDU2767 Proving Equivalences 解题报告【tarjan缩点】
- hdu 2460 Network Tarjan缩点+LCA 解题报告
- poj 2186 (tarjan 缩点)
- poj 2186 Popular Cows (tarjan + 缩点)
- POJ 2186 Popular Cows -- tarjan 缩点
- 【tarjan缩点+小拓展】【POJ-2186】
- tarjan缩点模板 poj 2186
- POJ 2186 popular cows (tarjan + 缩点)
- POJ 2186 Popular Cows 【Tarjan+缩点】
- hdu 2242 考研路茫茫——空调教室 【Tarjan缩点+树上DP】 解题报告
- BZOJ 1589 [Usaco2008 Dec] Tarjan缩点+记忆化搜索 解题报告
- hdu 1827 Summer Holiday Tarjan缩点+度数判断 解题报告
- HDU1827 Summer Holiday 解题报告【tarjan/强连通分量+缩点】
- HDU2767 Tarjan 解题报告
- POJ 2186 解题报告
- POJ 2942 Tarjan双联通分量+二分图 解题报告
- 熄灯问题
- 算法十九
- Linux中 Mysql 14.14 数据库编码修改为utf-8
- error while loading shared libraries: libvga.so.1: cannot open shared object file: No such file or
- android Retrofit的简单使用
- POJ 2186 Tarjan 缩点 解题报告
- PAT 1003:Emergency解题报告
- 创建和销毁对象
- nodeJS基础--模块加载机制
- java每日一练
- 莫比乌斯反演相关
- acvitity和fragment中都可以单独设置的沉浸式状态栏的自定义实现方式(包括白底黑字状态栏)
- cookie安全加密的哪些事
- [深入理解JVM 六]---虚拟机字节码执行系统