poj2186Popular Cows(强连通+缩点)

来源:互联网 发布:装修自动设计软件 编辑:程序博客网 时间:2024/04/25 23:44

Popular Cows
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 19647 Accepted: 7960

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 31 22 12 3

Sample Output

1

Hint

Cow 3 is the only cow of high popularity. 

Source

USACO 2003 Fall

题目大意:给一张图,求所有点可达的点的个数。

题目分析:先求一次强连通将有环图转化成一个DAG,然后缩点,求新图中出度为0的点的个数,如果出度为0的点只有1个则这个强连通所有点都能被其他点可达,否则没有点能被所有点可达。

详情请见代码:

#include <iostream>#include<cstdio>#include<cstring>using namespace std;const int N = 10005;const int M = 50005;struct edge{    int to,next;}g[M];int head[N];int scc[N];int cnt[N];//统计强连通的点数int stack1[N];int stack2[N];int vis[N];int out[N];int m,n;void init(){    int i;    for(i = 1;i <= n;i ++)    {        head[i] = -1;        scc[i] = cnt[i] = vis[i] = out[i] = 0;    }}int nextint(){    char c;    int ret;    while((c = getchar()) > '9' || c < '0')        ;    ret = c - '0';    while((c = getchar()) >= '0' && c <= '9')        ret = ret * 10 + c - '0';    return ret;}void dfs(int cur,int &sig,int &ret){    vis[cur] = ++sig;    stack1[++stack1[0]] = cur;    stack2[++stack2[0]] = cur;    for(int i = head[cur];i != -1;i = g[i].next)    {        if(vis[g[i].to] == 0)            dfs(g[i].to,sig,ret);        else            if(scc[g[i].to] == 0)            {                while(vis[stack2[stack2[0]]] > vis[g[i].to])                    stack2[0] --;            }    }    if(stack2[stack2[0]] == cur)    {        ++ret;        stack2[0] --;        do        {            scc[stack1[stack1[0]]] = ret;            cnt[ret] ++;        }while(stack1[stack1[0] --] != cur);    }}int Gabow(){    int i,sig,ret;    stack1[0] = stack2[0] = sig = ret = 0;    for(i = 1;i <= n;i ++)        if(!vis[i])            dfs(i,sig,ret);    return ret;}int solve(){    int ret,i,j;    int num = Gabow();    if(num == 1)        return n;    for(i = 1;i <= n;i ++)    {        for(j = head[i];j != -1;j = g[j].next)        {            if(scc[i] != scc[g[j].to])                out[scc[i]] ++;        }    }    int ct = 0;    int ci;    for(i = 1;i <= num;i ++)    {        if(out[i] == 0)        {            ct ++;            ci = i;        }    }    if(ct == 1)        return cnt[ci];    else        return 0;}int main(){    int i,j,a,b;    while(~scanf("%d%d",&n,&m))    {        init();        for(i = 1;i <= m;i ++)        {            a = nextint();            b = nextint();            g[i].to = b;            g[i].next = head[a];            head[a] = i;        }        printf("%d\n",solve());    }    return 0;}//1468K16MS  G++//824K110MS C++




原创粉丝点击