Poj 2186- Popular Cows//kosaraju

來源:互聯網
上載者:User
Popular Cows
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 19089   Accepted: 7678

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

分析:剛開始理解錯題意了,以為是找出最受歡迎的牛,結果發現不是。就是找到每頭牛都覺得該頭牛牛x的個數。也就是在一個DAG中找出出度為零的點,如果有兩個出度為零的點,則不可能有這樣的一頭牛。

方法:一、求強連通分支,將圖轉為DAG

            二、在DAG中找到出度為零的連通分支或點。

代碼:

#include<cstdio>#include<cstring>#include<vector>using namespace std;const int maxn = 10010;vector<int> G[maxn],Gt[maxn];int ord[maxn];int vis[maxn];int out[maxn]; int ss[maxn];int ans[maxn];int cnt;int t;int n,m;void dfs_1(int u){    vis[u] = 1;    for(int i = 0; i < G[u].size(); i++)    {        int v = G[u][i];        if(!vis[v]) dfs_1(v);    }    ord[t++] = u;}void dfs_2(int u){    vis[u] = 1;    ss[u] = cnt;    for(int i = 0; i < Gt[u].size(); i++)    {        int v = Gt[u][i];        if(!vis[v])        {            ans[cnt]++;            dfs_2(v);        }    }}void kosaraju(){    int flag = -1;    t = 1;    cnt = 1;    for(int i = 1; i <= n; i++) ans[i] = 1;    memset(out,0,sizeof(out));    memset(vis,0,sizeof(vis));    for(int i = 1; i <= n; i++) if(!vis[i]) dfs_1(i);    memset(vis,0,sizeof(vis));    for(int i = t - 1; i >= 1; i--)    {        int v = ord[i];        if(!vis[v])        {            dfs_2(v);            cnt++;        }    }    //構造DAG    for(int i = 1; i <= n; i++)    {        for(int j = 0; j < G[i].size(); j++)        {            if(ss[i] == ss[G[i][j]]) continue;            out[ss[i]]++;        }    }    //尋找出度為零的點或者連通分支    for(int i = 1; i < cnt; i++)    {        if(!out[i] && flag == -1) flag = ans[i];        else if(!out[i] && flag != -1)        {            flag = 0;            break;        }    }    printf("%d\n",flag);}int main(){    while(scanf("%d%d",&n,&m) != EOF)    {        for(int i = 1; i <= m; i++)        {            int u,v;            scanf("%d%d",&u,&v);            G[u].push_back(v);            Gt[v].push_back(u);        }        kosaraju();    }    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.