zoukankan      html  css  js  c++  java
  • POJ 2186 Popular Cows

                        Popular Cows

    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 32017   Accepted: 13039

    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
    题目大意:有很多奶牛,很多仰慕关系,仰慕关系存在传递性,求所有的牛都仰慕的对象有多少。。
     1 #include<iostream>
     2 #include<cstring>
     3 #include<cstdio>
     4 #include<stack>
     5 #define maxn 10005
     6 #define maxm 50010
     7 using namespace std;
     8 int n,m;
     9 struct edge{
    10     int u,v,next;
    11 }e[maxm],ee[maxm];
    12 int head[maxn],js,headd[maxn],jss,bianshu;
    13 bool exist[maxn];
    14 int visx,cur;// cur--缩出的点的数量 
    15 int dfn[maxn],low[maxn],belong[maxn],chudu[maxn],rudu[maxn];
    16 stack<int>st;
    17 void add_edge(int u,int v){
    18     e[++js].u=u;e[js].v=v;
    19     e[js].next=head[u];head[u]=js;
    20 }
    21 void add_edgee(int u,int v){
    22     ee[++jss].u=u;ee[jss].v=v;
    23     ee[jss].next=head[u];head[u]=jss;
    24 }
    25 void tarjan(int u){
    26     dfn[u]=low[u]=++visx;
    27     st.push(u);exist[u]=true;
    28     for(int i=head[u];i;i=e[i].next){
    29         int v=e[i].v;
    30         if(dfn[v]==0) {
    31             tarjan(v);
    32             low[u]=min(low[v],low[u]);
    33         }
    34         else if(exist[v]&&low[u]>dfn[v]) low[u]=dfn[v];
    35     }
    36     int j;
    37     if(low[u]==dfn[u]){
    38         ++cur;
    39         do{
    40             j=st.top();st.pop();
    41             exist[j]=false;belong[j]=cur;
    42             rudu[cur]++;
    43         }while(j!=u);
    44     }
    45 }
    46 int main()
    47 {
    48     scanf("%d%d",&n,&m);
    49     int u,v;
    50     for(int i=1;i<=m;i++){
    51         scanf("%d%d",&u,&v);
    52         add_edge(u,v);
    53     }
    54     for(int i=1;i<=n;i++)
    55       if(dfn[i]==0)
    56         tarjan(i);      
    57     for(int i=1;i<=js;i++){
    58         int u=e[i].u,v=e[i].v;
    59         if(belong[u]!=belong[v]){
    60             add_edgee(belong[u],belong[v]);
    61             chudu[belong[u]]++;
    62         }
    63     }
    64     int chu=0,ru=0;
    65     for(int i=1;i<=cur;i++){
    66         if(chudu[i]==0) chu++,bianshu=i;
    67     }
    68     if(chu==1) printf("%d
    ",rudu[bianshu]);
    69     else printf("%d
    ",0);
    70     return 0;
    71 }

    思路:首先强连通分量中的奶牛都互相仰慕,所以进行缩点,然后求出出度为零的点的个数,若有一个,则就是答案(这个强连通分量的点的个数),若不止一个,则无答案。。。//我用rudu数组记录的每个强连通分量里点的个数。。。

  • 相关阅读:
    一个list<Map>里map其中的一个字段的值相同,如何判断这个字段相同,就把这个map的其他字段存入另一个map中
    ES6---箭头函数()=>{} 与function的区别
    VSCode代码格式化快捷键及保存时自动格式化
    NoClassDefFoundError: com/fasterxml/jackson/databind/exc/InvalidDefinitionException
    响应数据中文乱码
    tomcat将静态资源写入到浏览器乱码问题
    mark一款全局搜索工具,可以搜索文本内容
    mpvue 开发微信小程序搭建项目
    h5返回上一页ios页面不刷新
    微信小程序去除页面滚动条
  • 原文地址:https://www.cnblogs.com/suishiguang/p/6241183.html
Copyright © 2011-2022 走看看