(連通圖 縮點 強聯通分支)Popular Cows -- poj --2186

來源:互聯網
上載者:User

標籤:

http://poj.org/problem?id=2186

 

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

 

先縮點(把強聯通分量看為一個點), 判斷出度為 0 的點有幾個,如果大於 1 則輸出 0, 否則輸出 出度為零的點的個數

 

代碼:

#include<cstdio>#include<cstring>#include<cstdlib>#include<iostream>#include<queue>#include<stack>#include<algorithm>using namespace std;#define N 50005struct node{    int v, next;}a[N];int Head[N], cnt;int dfn[N], low[N], Time, bnt, belong[N];int Stack[N], InStack[N], top;void Init(){    cnt = Time = bnt = top = 0;    memset(Head, -1, sizeof(Head));    memset(dfn, 0, sizeof(dfn));    memset(low, 0, sizeof(low));    memset(Stack, 0, sizeof(Stack));    memset(InStack, 0, sizeof(InStack));}void Add(int u, int v){    a[cnt].v = v;    a[cnt].next = Head[u];    Head[u] = cnt++;}void Tarjar(int u){    int v;    low[u] = dfn[u] = ++Time;    InStack[u] = 1;    Stack[top++] = u;    for(int j=Head[u]; j!=-1; j=a[j].next)    {        v = a[j].v;        if(!dfn[v])        {            Tarjar(v);            low[u] = min(low[u], low[v]);        }        else if(InStack[v])            low[u] = min(low[u], dfn[v]);    }    if(dfn[u]==low[u])    {        bnt++;        do        {            v = Stack[--top];            InStack[v] = 0;            belong[v] = bnt;        }while(u!=v);    }}int main(){    int n, m;    while(scanf("%d%d", &n, &m)!=EOF)    {        int i, u, v;        Init();        for(i=1; i<=m; i++)        {            scanf("%d%d", &u, &v);            Add(u, v);        }        for(i=1; i<=n; i++)        {            if(!dfn[i])            Tarjar(i);        }        int Out[N]={0};        for(int i=1; i<=n; i++)        {            for(int j=Head[i]; j!=-1; j=a[j].next)            {                u = belong[i], v = belong[a[j].v];                if(u!=v)                    Out[u]++;            }        }        int flag=0, Index;        for(i=1; i<=bnt; i++)        {            if(!Out[i])            {                flag++;                Index = i;            }        }        if(flag>1)            printf("0\n");        else        {            int ans = 0;            for(i=1; i<=n; i++)            {                if(belong[i]==Index)                    ans++;            }            printf("%d\n", ans);        }    }    return 0;}

 

(連通圖 縮點 強聯通分支)Popular Cows -- poj --2186

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.