poj 2186 Popular Cows(Tarjan)
2015-03-28 00:09
344 查看
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
Sample Output
Hint
Cow 3 is the only cow of high popularity.
Source
USACO 2003 Fall
给定牛之间的仰慕关系 且仰慕是可传递的 问 能受到所有牛仰慕的牛的数目。。
如果要受到所有牛的仰慕 则这个牛的点对于其他牛来说 都是可达的
首先对图进行缩点 得到DAG 如果在该DAG中只有一个叶子节点
则这个节点 都可以由其他节点到达 输出这个点所代表的联通分量中点的个数
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 24548 | Accepted: 10072 |
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.
Source
USACO 2003 Fall
给定牛之间的仰慕关系 且仰慕是可传递的 问 能受到所有牛仰慕的牛的数目。。
如果要受到所有牛的仰慕 则这个牛的点对于其他牛来说 都是可达的
首先对图进行缩点 得到DAG 如果在该DAG中只有一个叶子节点
则这个节点 都可以由其他节点到达 输出这个点所代表的联通分量中点的个数
#include <cstdio> #include <iostream> #include <cstring> #include <cmath> #include <algorithm> #include <string.h> #include <string> #include <vector> #include <queue> #define MEM(a,x) memset(a,x,sizeof a) #define eps 1e-8 #define MOD 10009 #define INF 99999999 #define ll __int64 #define bug cout<<"here"<<endl #define fread freopen("ceshi.txt","r",stdin) #define fwrite freopen("out.txt","w",stdout) using namespace std; void read(int &x) { char ch; x=0; while(ch=getchar(),ch!=' '&&ch!='\n') { x=x*10+ch-'0'; } } const int MAXN=10010; const int MAXM=50010; struct Edge { int to,next; }edge[MAXM]; int head[MAXN],tot; int low[MAXN],dfn[MAXN],sta[MAXN],belong[MAXN]; int index,top; int scc; bool instack[MAXN]; int num[MAXN]; int n,m; void addedge(int u,int v) { edge[tot].to=v; edge[tot].next=head[u]; head[u]=tot++; } void Tarjan(int u) { // bug; int v; low[u]=dfn[u]=++index; sta[top++]=u; instack[u]=1; for(int i=head[u];i!=-1;i=edge[i].next) { // bug; v=edge[i].to; if(!dfn[v]) { Tarjan(v); low[u]=min(low[u],low[v]); } else if(instack[v]) low[u]=min(low[u],dfn[v]); } // bug; if(low[u]==dfn[u]) { scc++; do{ v=sta[--top]; instack[v]=0; belong[v]=scc; num[scc]++; }while(u!=v); } } int cd[MAXN]; void solve() { // bug; MEM(dfn,0); MEM(instack,0); MEM(cd,0); MEM(num,0); index=scc=top=0; for(int i=1;i<=n;i++) if(!dfn[i]) Tarjan(i); for(int u=1;u<=n;u++) { for(int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].to; if(belong[u]!=belong[v]) cd[belong[u]]++; } } int cnt=0; int x; for(int i=1;i<=scc;i++) { if(cd[i]==0) { cnt++; x=i; } } if(cnt==1) printf("%d\n",num[x]); else puts("0"); } void init() { tot=0; MEM(head,-1); } int main() { // fread; while(scanf("%d%d",&n,&m)!=EOF) { init(); int u,v; for(int i=0;i<m;i++) { scanf("%d%d",&u,&v); addedge(u,v); } solve(); } return 0; }
相关文章推荐
- poj 2186 Popular Cows(强连通分量模板题,tarjan)
- Popular Cows poj 2186 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,Tarjan,Gabow算法的理解,POJ 2186 Popular Cows(强连通分量)
- poj 2186 Popular Cows (强连通分量 tarjan)
- Popular Cows - POJ 2186 Tarjan
- poj 2186 Popular cows ( tarjan )
- POJ2186 Popular Cows 强连通分量tarjan
- POJ-2186 Popular Cows,tarjan缩点找出度为0的点。
- 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 强连通分量 缩点)
- (tarjan缩点)poj 2186 Popular Cows
- poj 2186 Popular Cows (tarjan缩点)