POJ 2186 -- Popular Cows (Strongly Connected Components)
来源:互联网 发布:多媒体文件播放软件 编辑:程序博客网 时间:2024/06/05 13:22
Problem: http://poj.org/problem?id=2186
缩点 http://en.wikipedia.org/wiki/Vertex_contraction#Vertex_identification
强连通分量
http://en.wikipedia.org/wiki/Strongly_connected_component
http://en.wikipedia.org/wiki/Kosaraju%27s_algorithm
http://en.wikipedia.org/wiki/Tarjan%27s_strongly_connected_components_algorithm
http://en.wikipedia.org/wiki/Path-based_strong_component_algorithm
Popular Cows
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 22027 Accepted: 8993
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.
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.
* 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 31 22 12 3
Sample Output
1
Hint
Cow 3 is the only cow of high popularity.
Source
USACO 2003 Fall
Kosaraju's algorithm works as follows:
- Let G be a directed graph and S be an empty stack.
- While S does not contain all vertices:
- Choose an arbitrary vertex v not in S. Perform a depth-first search starting at v. Each time that depth-first search finishes expanding a vertex u, push u onto S.
- Reverse the directions of all arcs to obtain the transpose graph.
- While S is nonempty:
- Pop the top vertex v from S. Perform a depth-first search starting at v in the transpose graph. The set of visited vertices will give the strongly connected component containing v; record this and remove all these vertices from the graph G and the stack S. Equivalently, breadth-first search (BFS) can be used instead of depth-first search.
C++ Code:
#include <cstdio>#include <set>#include <stack>#include <vector>using namespace std;void dfs(const int u, const vector<vector<int> > &adj, vector<bool> &flg, stack<int> &stk) {flg[u] = true;const int k = adj[u].size();for (int i = 0; i < k; ++i) {const int v = adj[u][i];if (!flg[v]) dfs(v, adj, flg, stk);}stk.push(u);}void scc(const int u, const int cnt, const vector<vector<int> > &rev, vector<bool> &flg, vector<int> &color) {flg[u] = true;color[u] = cnt;const int k = rev[u].size();for (int i = 0; i < k; ++i) {const int v = rev[u][i];if (!flg[v]) scc(v, cnt, rev, flg, color);}}int main() {int n, m;scanf("%d%d", &n, &m);vector<vector<int> > adj(n, vector<int>());vector<vector<int> > rev(n, vector<int>());while (m--) {int a, b;scanf("%d%d", &a, &b);--a, --b;adj[a].push_back(b);rev[b].push_back(a);}stack<int> stk;vector<bool> flg(n, false);for (int i = 0; i < n; ++i) {if (flg[i]) continue;dfs(i, adj, flg, stk);}flg.clear();flg.resize(n, false);vector<int> color(n, -1);int cnt = 0;while (!stk.empty()) {const int u = stk.top();stk.pop();if (flg[u]) continue;scc(u, cnt, rev, flg, color);++cnt;}vector<int> num(cnt, 0);for (int i = 0; i < n; ++i)++num[color[i]];vector<set<int> > edge(cnt, set<int>());for (int i = 0; i < n; ++i) {const int k = adj[i].size();for (int j = 0; j < k; ++j)if (color[i] != color[adj[i][j]])edge[color[i]].insert(color[adj[i][j]]);}int ans = 0;for (int i = 0; i < cnt; ++i) {if (edge[i].empty()) {if (ans == 0)ans = num[i];else {ans = 0;break;}}}printf("%d\n", ans);return 0;}
0 0
- POJ 2186 -- Popular Cows (Strongly Connected Components)
- Strongly Connected Components
- POJ 2186 Popular Cows
- poj 2186 Popular Cows
- poj 2186 Popular Cows
- poj 2186 Popular Cows
- poj 2186 Popular Cows
- poj 2186 Popular Cows
- POJ 2186 Popular Cows
- poj 2186 Popular Cows
- poj 2186 Popular Cows
- POJ 2186(Popular Cows)
- POJ 2186 Popular Cows
- poj 2186 Popular Cows
- poj 2186 Popular Cows
- POJ 2186 Popular Cows
- POJ 2186: Popular Cows
- poj 2186 Popular Cows
- KMP算法详解(转)
- css menu drop
- 查看当前系统的apache版本
- The Heartbleed Bug
- Matlab之print,fprint,fscanf,disp函数
- POJ 2186 -- Popular Cows (Strongly Connected Components)
- 【C++再学习】【01】新的开始
- HDU1560:DNA sequence(IDA星)
- django添加admin管理
- Vi编辑器使用和虚拟机共享文件方法
- 3d打印相关
- tomcat启动startup.bat一闪而过
- 制度化管理不等于流程化管理
- mfc下的人脸检测并保存截图