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 }
  • 相关阅读:
    [io PWA] keynote: Launching a Progressive Web App on Google.com
    hdu 4491 Windmill Animation
    BNU Concentric Rings
    Android 如何去掉手机中横竖屏切换时的转屏动画?
    IOS NSString 用法详解
    MySQL有关1042 Can’t get hostname for your address的问题分析解决过程
    网站出现问题时,站长该如何解决
    [置顶] IOS用CGContextRef画各种图形(文字、圆、直线、弧线、矩形、扇形、椭圆、三角形、圆角矩形、贝塞尔曲线、图片)
    具备这5点因素,你就是一名合格的网络编辑
    IOS 新消息通知提示-声音、震动
  • 原文地址:https://www.cnblogs.com/Comfortable/p/8678400.html
Copyright © 2011-2022 走看看