poj2186-Popular Cows

来源:互联网 发布:rc4算法原理图 编辑:程序博客网 时间:2024/06/06 00:19

Popular Cows
Time Limit: 2000MS
Memory Limit: 65536K
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.
Source
USACO 2003 Fall

问题本质是求强连通分量

#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>using namespace std;const int MAXN = 1000000 + 10;int tot, end_s = 0, color = 0;int sizeCol[MAXN], vis[MAXN], end[MAXN], col[MAXN];struct Edge{    int t, next;}map[MAXN];void addEdge(int x, int y) {    map[++tot].t = y;    map[tot].next = map[x].next;    map[x].next = tot;}void dfs1(int u) {    vis[u] = 1;    for (int i = map[u].next; i != -1; i = map[i].next) {      int v = map[i].t;      if (vis[v]) continue;      dfs1(v);    }    end[++end_s] = u;}void dfs2(int u) {    sizeCol[color]++;    col[u] = color;    for (int i = map[u].next; i != -1; i = map[i].next) {      int v = map[i].t;      if (col[v]) continue;      dfs2(v);    }}int x[MAXN], y[MAXN], out[MAXN];int main(){    int n, m;    while(~scanf("%d%d", &n, &m)) {        //init----------------------        memset(out, 0, sizeof out);        memset(col, 0, sizeof col);        memset(end, 0, sizeof end);        memset(vis, 0, sizeof vis);        memset(sizeCol, 0, sizeof sizeCol);        tot = n, end_s = 0, color = 0;        for (int i = 1; i <= n; ++i)          map[i].next = -1, map[i].t = i;        //--------------------------        for (int i = 1; i <= m; ++i) {          scanf("%d%d", &x[i], &y[i]);          addEdge(x[i], y[i]);        }        //第一次dfs,若不能遍历全图,则不可能         for (int i = 1; i <= n; ++i)          if (!vis[i])            dfs1(i);        //按出栈顺序dfs,缩点        for (int i = 1; i <= n; ++i) {          if (!col[end[i]]) {            ++color;            dfs2(end[i]);           }        }        //更新每一块点的出度         for (int i = 1; i <= m; ++i)          if (col[x[i]] != col[y[i]])            out[col[x[i]]]++;        int flag = 0, ans;        for (int i = 1; i <= color; ++i) {          if (!out[i] && !flag) {            flag = 1;            ans = sizeCol[i];          }          else if (!out[i] && flag) {            ans = 0;            break;          }        }        printf("%d\n", ans);    }    return 0;}
原创粉丝点击