Popular Cows(POJ 2186)

  • 原题如下:
    Popular Cows
    Time Limit: 2000MS Memory Limit: 65536K
    Total Submissions: 40746 Accepted: 16574

    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. 
  • 题解:建图显然,假设两头牛A和B都被其他所有牛认为是红人,那么显然A和B互相认为对方是红人,即存在一个包含A、B两个顶点的圈,或者说,A、B同属于一个强连通分量,反之,如果一头牛被其他所有牛认为是红人,那么其所属的强连通分量内的所有牛都被其他所有牛认为是红人。由此可知,把图进行强连通分量分解后,至多有一个强连通分量满足题目的条件。而进行强连通分解时,我们还可以得到各个强连通分量拓扑排序后的顺序,唯一可能成为解的只有拓扑序最后的强连通分量,,所以在最后,我们只要检查最后一个强连通分量是否从所有顶点可达就好了。该算法的复杂度为O(N+M)。
  • 代码:
     1 #include <cstdio>
     2 #include <stack>
     3 #include <vector>
     4 #include <algorithm>
     5 #include <cstring>
     6 
     7 using namespace std;
     8 
     9 stack<int> s;
    10 const int MAX_V=11000;
    11 bool instack[MAX_V];
    12 int dfn[MAX_V];
    13 int low[MAX_V];
    14 int ComponentNumber=0;
    15 int index; 
    16 vector<int> edge[MAX_V];
    17 vector<int> redge[MAX_V];
    18 vector<int> Component[MAX_V];
    19 int inComponent[MAX_V];
    20 int N, M;
    21 bool visited[MAX_V];
    22 
    23 void add_edge(int x, int y)
    24 {
    25     edge[x].push_back(y);
    26     redge[y].push_back(x);
    27 }
    28 
    29 void tarjan(int i)
    30 {
    31     dfn[i]=low[i]=index++;
    32     instack[i]=true;
    33     s.push(i);
    34     int j;
    35     for (int e=0; e<edge[i].size(); e++)
    36     {
    37         j=edge[i][e];
    38         if (dfn[j]==-1)
    39         {
    40             tarjan(j);
    41             low[i]=min(low[i], low[j]);
    42         }
    43         else 
    44             if (instack[j]) low[i]=min(low[i], dfn[j]);
    45     }
    46     if (dfn[i]==low[i])
    47     {
    48         ComponentNumber++;
    49         do
    50         {
    51             j=s.top();
    52             s.pop();
    53             instack[j]=false;
    54             Component[ComponentNumber].push_back(j);
    55             inComponent[j]=ComponentNumber;
    56         }
    57         while (j!=i);
    58     }
    59 }
    60 
    61 void rdfs(int v)
    62 {
    63     visited[v]=true;
    64     for (int i=0; i<redge[v].size(); i++)
    65     {
    66         if (!visited[redge[v][i]])
    67         {
    68             rdfs(redge[v][i]);
    69         }
    70     }
    71 }
    72 
    73 int main()
    74 {
    75     memset(dfn, -1, sizeof(dfn));
    76     scanf("%d %d", &N, &M);
    77     for (int i=0; i<M; i++)
    78     {
    79         int x, y;
    80         scanf("%d %d", &x, &y);
    81         add_edge(x, y);
    82     }
    83     for (int i=1; i<N+1; i++)
    84     {
    85         if (dfn[i]==-1) tarjan(i);
    86     }
    87     int v=Component[1][0];
    88     int num=Component[1].size();
    89     rdfs(v);
    90     for (int i=1; i<=N; i++)
    91     {
    92         if (!visited[i]) 
    93         {
    94             num=0;
    95             break;
    96         }
    97     }
    98     printf("%d\n", num);
    99 }

     

转载于:https://www.cnblogs.com/Ymir-TaoMee/p/9792105.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值