poj2086——Popular Cows(强连通分量)
来源:互联网 发布:linux怎么安装软件命令 编辑:程序博客网 时间:2024/05/01 23:12
Popular Cows
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 18349Accepted: 7390
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
Hint
Cow 3 is the only cow of high popularity.
USACO 2003 Fall
解析:
根据题意建立有向无环图,用tarjan求强连通分量,缩点。。。
再重新建图,维护出度,找出度为零的点的个数。。。
本题是一道较裸的求强连通分量的题。。。
代码:
#include<cstdio>#include<iostream>#include<algorithm>using namespace std;const int N=10010;const int M=50010;int l=0;struct status{ int to,next;}node1[M],node2[M];int head[N],first[N];int low[N],dfsnum[N],stack[N];int n,m,stamp,cnt,top,e=0;bool vis[N];int cnt_size[N],belong[N],outdegree[N];void add1(int u,int v){ node1[l].to=v; node1[l].next=head[u]; head[u]=l++;}void tarjan(int u){ dfsnum[u]=low[u]=++stamp; vis[u]=true; stack[top++]=u; for(int i=head[u];i!=-1;i=node1[i].next) { int v=node1[i].to; if(!dfsnum[v]) { tarjan(v); low[u]=min(low[u],low[v]); } else if(vis[v])low[u]=min(dfsnum[v],low[u]); } if(dfsnum[u]==low[u]) { do { top--; vis[stack[top]]=false; cnt_size[cnt]++; belong[stack[top]]=cnt; } while(stack[top]!=u); cnt++; }}void add2(int u,int v){ node2[e].to=v; node2[e].next=first[u]; first[u]=e++;}void work(){ freopen("xx.in","r",stdin); freopen("xx.out","w",stdout); scanf("%d%d",&n,&m); l=0; memset(head,-1,sizeof(head)); for(int i=1;i<=m;i++) { int u,v; scanf("%d%d",&u,&v); add1(u,v); //邻接表建图 } memset(dfsnum,0,sizeof(dfsnum));memset(vis,0,sizeof(vis));stamp=cnt=top=0;memset(cnt_size,0,sizeof(cnt_size));memset(low,0,sizeof(low)); for(int i=1;i<=n;i++) { if(!dfsnum[i])tarjan(i); //tarjan找强连通分量,缩点 } memset(first,-1,sizeof(first)); for(int i=1;i<=n;i++) for(int j=head[i];j!=-1;j=node1[j].next) { int v=node1[j].to; if(belong[i]!=belong[v]) { add2(belong[i],belong[v]); //重新建图 } } memset(outdegree,0,sizeof(outdegree)); for(int i=0;i<cnt;i++) { for(int j=first[i];j!=-1;j=node2[j].next) outdegree[i]++; //维护点的出度 } int num=0; int sum=0; for(int i=0;i<cnt;i++) { if(outdegree[i]==0) //找出度为零的节点个数 { sum=cnt_size[i]; num++; } } if(num==1)printf("%d\n",sum); else printf("0\n"); }int main(){ work(); return 0;}
- poj2086——Popular Cows(强连通分量)
- POJ2186 Popular Cows(强连通分量)
- poj_2186 Popular Cows(强连通分量)
- poj2186 Popular Cows(强连通分量)
- POJ 2186 Popular Cows (强连通分量)
- POJ 2186 Popular Cows(强连通分量)
- POJ 2186 Popular Cows (强连通分量)
- poj 2186 Popular Cows(强连通分量)
- POJ-2186 Popular Cows (强连通分量[Kosaraju])
- POJ 2186 Popular Cows(强连通分量)
- POJ 2186 Popular Cows(强连通分量分解)
- Popular Cows poj 2186(强连通分量+缩点 )
- POJ 2186 Popular Cows(强连通分量Tarjan算法)
- (POJ 2186)Popular Cows 强连通分量 + 缩点图
- POJ 2186 Popular Cows(强连通分量模板题)
- 【POJ 2186】Popular Cows(强连通分量_tarjan)
- POJ 2186 Popular Cows(强连通分量分解)
- 图论:POJ2186-Popular Cows (求强连通分量)
- NodeJS中module.exports与exports的区别
- 成语接龙游戏网址
- PHP常用的缓存技术
- 我所热衷的编程生涯 连载(6)
- java 抽象类实现接口
- poj2086——Popular Cows(强连通分量)
- 关于创业的一点感想
- 关于HTML5的十条霸气侧漏预测
- rt-thread中用消息队列实现广播功能的一种方法
- #include<cstdio>
- 使用Django与Piston构建RESTful WebService及Django
- Android Camera 使用小结
- bootcmd 和bootargs【转】
- MMNL absent for 312 secs; Foregrounds taking over