zoukankan      html  css  js  c++  java
  • POJ 2186

    Popular Cows
    Time Limit: 2000MSMemory Limit: 65536K
    Total Submissions: 21823Accepted: 8900

    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

    ps:简单的强连通缩点。理解就好
     1 //强连通
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 #include<vector>
     6 using namespace std;
     7 const int MAX = 50000+10;
     8 int vis[MAX]; int sccno[MAX];
     9 vector<int> G[MAX],vs,RG[MAX];
    10 void add_edge(int from,int to)
    11 {
    12     G[from].push_back(to);
    13     RG[to].push_back(from);
    14 }
    15 void dfs(int u)
    16 {
    17     if(vis[u]) return ;
    18     vis[u]=1;
    19     for(int i=0;i<G[u].size();i++)
    20     {
    21         if(!vis[G[u][i]]) dfs(G[u][i]);
    22     }
    23     vs.push_back(u);
    24 }
    25 void rdfs(int u,int k)
    26 {
    27     if(sccno[u]) return ;
    28     sccno[u]=k;
    29     for(int i=0;i<RG[u].size();i++)
    30     {
    31         if(!sccno[RG[u][i]]) rdfs(RG[u][i],k);
    32     }
    33 }
    34 int main()
    35 {
    36     int n,m; int a,b,cnt;
    37     while(scanf("%d %d",&n,&m)==2)
    38     {
    39         for(int i=0;i<m;i++)
    40         {
    41             scanf("%d %d",&a,&b);
    42             add_edge(a,b);
    43         }
    44         memset(vis,0,sizeof(vis));
    45         memset(sccno,0,sizeof(sccno));
    46         for(int i=1;i<=n;i++) dfs(i);
    47         cnt=0;
    48         for(int i=n-1;i>=0;i--)
    49         {
    50             if(!sccno[vs[i]])
    51             {
    52                 cnt++;
    53                 rdfs(vs[i],cnt);
    54             }
    55         }
    56         int u,ans=0;
    57         for(int i=1;i<=n;i++)
    58         {
    59             if(sccno[i]==cnt)
    60             {
    61                 u=i;
    62                 ans++;
    63             }
    64         }
    65 
    66         memset(sccno,0,sizeof(sccno));
    67         rdfs(u,1); int flag=1;
    68         for(int i=1;i<=n;i++)
    69         {
    70             if(!sccno[i]) flag=0;
    71         }
    72         if(flag) printf("%d ",ans);
    73         else printf("0 ");
    74     }
    75     return 0;
    76 }
  • 相关阅读:
    fastjson-alibaba
    ubuntu 入门
    资料文档
    asp.net mvc View视图相关
    视频
    js第三方
    工具类网址
    [转]初学者的编程自学指南
    seajs的使用--主要了解模块化
    其它
  • 原文地址:https://www.cnblogs.com/acvc/p/3612167.html
Copyright © 2011-2022 走看看