zoukankan      html  css  js  c++  java
  • [Tarjan] Poj P2186 Popular Cows

    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

    题意

    有N只奶牛,其中奶牛A认为奶牛B备受注目,而奶牛B也可能认为奶牛C备受注目。奶牛们的这种“认为”是单向可传递的,就是说若奶牛A认为奶牛B备受注目,但奶牛B不一定会认为奶牛A备受注目。而当A认为B备受注目,且B认为C备受注目时,A一定也认为C备受注目。

    题解

    • 如果一头牛有出度,那么它就不是被其他所有牛仰慕的牛
    • 如果其出度为0那么其有可能成为被其他所有牛仰慕的牛
    • 但是当出度为0的牛超过1时,便不存在被除自身外其他牛仰慕的牛
    • 因为肯定有另外一头出度为0的牛不仰慕它

    代码

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<algorithm>
     6 using namespace std;
     7 struct node{ int to,next; }q[50001];  
     8 int n,m,head[10001],dfn[10001],low[10001],stack1[10001],num[10001],du[10001],vis[10001],cnt,time=1,tot,cut;  
     9 int tim,tp,dcnt,sum;
    10 void insert(int u,int v)  
    11 {  
    12     q[cnt].to=v;  
    13     q[cnt].next=head[u];  
    14     head[u]=cnt;  
    15     cnt++;  
    16 }
    17 void tarjan(int u,int fa)  
    18 {  
    19     dfn[u]=time; low[u]=time;  
    20     time++;  
    21     vis[u]=1;
    22     stack1[tot]=u;  
    23     tot++;  
    24     for (int i=head[u];i!=-1;i=q[i].next)  
    25     {  
    26         int v=q[i].to;  
    27         if (!vis[v])  
    28         {  
    29             tarjan(v,u);
    30             low[u]=min(low[u],low[v]);  
    31         }  
    32         else if (vis[v]) low[u]=min(low[u],dfn[v]);  
    33     }  
    34     if (low[u]==dfn[u])  
    35     {  
    36         cut++;  
    37         while (tot>0&&stack1[tot]!=u)  
    38         {  
    39             tot--;
    40             vis[stack1[tot]]=2;  
    41             num[stack1[tot]]=cut;  
    42         }  
    43      }  
    44  }  
    45 int main()
    46 {     
    47     cin>>n>>m;
    48     memset(head,-1,sizeof(head));
    49     for (int i=0;i<=m-1;i++)
    50     {
    51         int a,b;
    52         cin>>a>>b;
    53         insert(a,b);
    54     }
    55     for (int i=1;i<=n;i++) if (!vis[i]) tarjan(i,0);
    56     for (int i=1;i<=n;i++)
    57         for(int j=head[i];j!=-1;j=q[j].next)
    58             if (num[i]!=num[q[j].to]) du[num[i]]++;
    59     int x;
    60     sum=0;
    61     for (int i=1;i<=cut;i++)
    62         if (!du[i])
    63         {
    64             sum++;
    65             x=i;
    66         }
    67     if (sum==1)
    68     {
    69         sum=0;
    70         for (int i=1;i<=n;i++) if (num[i]==x) sum++;
    71         cout<<sum<<endl;
    72     }
    73     else cout<<"0"<<endl;
    74     return 0;
    75 }
  • 相关阅读:
    spring-tool-suite-4下载安装及报错的解决办法
    CentOS 6.5使用yum快速搭建LAMP环境
    jquery省份城市选择器
    js 将json字符串转换为json对象的方法解析
    Spring MVC添加支持Http的delete、put请求!(HiddenHttpMethodFilter)
    Filebeat工作原理
    filebeat_config
    Docker设置http代理
    转-OWASP CSRFGuard使用细节
    转-JavaWeb三大组件之Listener监听器
  • 原文地址:https://www.cnblogs.com/Comfortable/p/8678400.html
Copyright © 2011-2022 走看看