poj 2186 Popular Cows
2013-03-29 10:37
253 查看
题目:
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.
代码:
Popular Cows
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 18659 | Accepted: 7516 |
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.
代码:
#include<iostream> #include<stdio.h> #include<stack> #include<string.h> using namespace std; #define MAXN 10010 struct Node { int v; int next; } edge[50010]; int head[MAXN]; int dfn[MAXN]; int low[MAXN]; bool mark[MAXN]; //to judge in stack or not int id[MAXN]; //the id of scc int T; stack<int> sta; int scc; void tarjan(int v) { dfn[v] = low[v] = ++T; sta.push(v); mark[v] = true; int k; for (k = head[v]; k != -1; k = edge[k].next) { int u = edge[k].v; if (!dfn[u]) { tarjan(u); low[v] = min(low[v], low[u]); } else if (mark[u]) low[v] = min(low[v], dfn[u]); } if (low[v] == dfn[v]) { ++scc; int u; do { u = sta.top(); sta.pop(); mark[u] = false; id[u] = scc; } while (u != v); } return; } void solve(int n) { scc = 0; int i; for (i = 1; i <= n; ++i) if (!dfn[i]) tarjan(i); } int main() { int n,m; scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) { head[i]=-1; mark[i]=false; dfn[i]=0; } scc=0; int a,b; for(int i=0;i<m;i++) { scanf("%d%d",&a,&b); edge[i].v=b; edge[i].next=head[a]; head[a]=i; } solve(n); memset(mark,false,sizeof(mark)); for(int i=1;i<=n;i++) { for(int j=head[i];j!=-1;j=edge[j].next) { if(id[i]!=id[edge[j].v]) { mark[id[i]]=true; } } } int ans=0; int idd; for(int i=1;i<=scc;i++) { if(!mark[i]) { ans++; idd=i; } } if(ans>1) { printf("0\n"); } else { ans=0; for(int i=1;i<=n;i++) { if(id[i]==idd) ans++; } printf("%d\n",ans); } return 0; }
相关文章推荐
- poj 2186 Popular Cows Tarjan 强连通分量
- poj2186——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
- POJ 2186 Popular Cows(Tarjan)
- POJ 2186 Popular Cows(缩点+出度判断)
- poj 2186 Popular Cows (Kosaraju+缩点)
- POJ 2186 Popular Cows 强连通分量 Kosaraju or tarjan
- poj2186 Popular Cows
- poj 2186 Popular Cows(Tarjan,强连通分量缩点)
- POJ-2186 Popular Cows (强连通缩点)
- poj 2186——Popular Cows
- poj 2186 Popular Cows【tarjan求scc个数&&缩点】【求一个图中可以到达其余所有任意点的点的个数】
- Popular Cows - POJ 2186 Tarjan
- poj 2186 Popular Cows(强连通分量,tarjan或Kosaraju)
- poj 2186: Popular Cows(tarjan基础题)