POJ 2186 Popular Cows
2013-05-27 21:41
357 查看
[align=center]Popular Cows[/align]
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
强连通分量,缩点都想到了,就是没有想到出度为0的必须只有一个,并且那个缩点对应的点的个数就是所求
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 19286 | Accepted: 7761 |
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
强连通分量,缩点都想到了,就是没有想到出度为0的必须只有一个,并且那个缩点对应的点的个数就是所求
/*************************************************************** > File Name: cows.cpp > Author: SDUT_GYX > Mail: 2272902662@qq.com > Created Time: 2013/5/27 19:46:53 **************************************************************/ #include <iostream> #include <cstring> #include <algorithm> #include <cstdio> #include <queue> #include <cstdlib> #include <iomanip> #include <string> #include <vector> #include <map> #include <cmath> #include <stack> #define LL long long #define N 10000 using namespace std; vector<int> pt[N+10]; stack<int> sta; int low[N+10],dfn[N+10],out[N+10],num[N+10],shift[N+10],cou,id; bool instack[N+10]; int main() { //freopen("data1.in","r",stdin); void Tarjan(int u); int n,m; while(scanf("%d %d",&n,&m)!=EOF) { for(int i=1; i<=n; i++) { pt[i].clear(); } while(m--) { int x,y; scanf("%d %d",&x,&y); pt[x].push_back(y); } memset(dfn,0,sizeof(dfn)); memset(low,0,sizeof(low)); cou=0; id=0; for(int i=1;i<=n ;i++) { if(!dfn[i]) { while(!sta.empty()) { sta.pop(); } memset(instack,false,sizeof(instack)); Tarjan(i); } } memset(out,0,sizeof(out)); int key,res; for(int i=1;i<=n;i++) { for(int j=1;j<=pt[i].size(); j++) { int v= pt[i][j-1]; if(shift[i]!=shift[v]) { out[shift[i]] += 1; } } } key = 0; for(int i=1;i<=id; i++) { if(out[i]==0&&key==0) { res = num[i]; key = 1; }else if(out[i]==0) { key = 2; } } if(key==1) { printf("%d\n",res); }else { printf("0\n"); } } return 0; } void Tarjan(int u) { low[u] = dfn[u] = ++cou; sta.push(u); instack[u] = true; for(int i=1; i<=pt[u].size(); i++) { int v = pt[u][i-1]; if(!dfn[v]) { 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]) { id++; int x,s=0; do { x = sta.top(); instack[x] = false; sta.pop(); shift[x] = id; s += 1; }while(x != u); num[id] = s; } }
相关文章推荐
- POJ 2186 Popular Cows (强连通分量)
- poj 2186 Popular Cows 强连通
- 【POJ】2186 - Popular Cowsa 强连通分量【模版】
- POJ 2186 Popular Cows
- POJ 2186 Popular Cows (强连通分量)
- poj 2186 Popular Cows
- poj 2186 Popular Cows
- poj_2186 Popular Cows(强连通分量)
- POJ 2186 Popular Cows -- tarjan 缩点
- poj 2186 Popular Cows(强连通分量缩点+dfs)
- POJ 2186 Popular Cows - from lanshui_Yang
- POJ 2186 Popular Cows(强连通)
- POJ 2186 Popular Cows
- 链式前向星,kosaraju,Tarjan,Gabow算法的理解,POJ 2186 Popular Cows(强连通分量)
- poj 2186 Popular Cows 强连通分量
- POJ 2186 Popular Cows
- POJ 2186 Popular Cows(强连通,kosaraju算法)
- poj 2186 Popular Cows(第一道强连通分支题)
- poj2186 Popular Cows
- poj 2186 Popular Cows