zoukankan      html  css  js  c++  java
  • POJ 2186 Popular Cows (强联通)

    http://poj.org/problem?

    id=2186

    Popular Cows
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 23819   Accepted: 9767

    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



    题意:

    一群牛中找被其它全部牛觉得是受欢迎的牛的数量。当中受欢迎有传递性。比方A觉得B受欢迎。B觉得C受欢迎,那么A觉得C也是受欢迎的。

    分析:

    假设某头牛是受欢迎的。那么从其它全部牛出发都能到达这头牛。假设用搜索做似乎太过复杂。首先进行强联通缩点。这样得到一个DAG,假设该DAG有且仅有一个出度为0的缩点点(极大强联通分量),那么这个缩点包含的牛的数量即为答案。


    /*
     *
     * Author : fcbruce <fcbruce8964@gmail.com>
     *
     * Time : Tue 14 Oct 2014 03:00:16 PM CST
     *
     */
    #include <cstdio>
    #include <iostream>
    #include <sstream>
    #include <cstdlib>
    #include <algorithm>
    #include <ctime>
    #include <cctype>
    #include <cmath>
    #include <string>
    #include <cstring>
    #include <stack>
    #include <queue>
    #include <list>
    #include <vector>
    #include <map>
    #include <set>
    #define sqr(x) ((x)*(x))
    #define LL long long
    #define itn int
    #define INF 0x3f3f3f3f
    #define PI 3.1415926535897932384626
    #define eps 1e-10
    
    #ifdef _WIN32
      #define lld "%I64d"
    #else
      #define lld "%lld"
    #endif
    
    #define maxm 50007
    #define maxn 10007
    
    using namespace std;
    
    int n,m;
    int fir[maxn];
    int u[maxm],v[maxm],nex[maxm];
    int e_max;
    
    int pre[maxn],low[maxn],sccno[maxn],w[maxn];
    int st[maxn],top;
    int scc_cnt,dfs_clock;
    
    int deg[maxn];
    
    inline void add_edge(int s,int t)
    {
      int e=e_max++;
      u[e]=s;v[e]=t;
      nex[e]=fir[u[e]];fir[u[e]]=e;
    }
    
    void tarjan_dfs(int s)
    {
      pre[s]=low[s]=++dfs_clock;
      st[++top]=s;
      for (int e=fir[s];~e;e=nex[e])
      {
        int t=v[e];
        if (pre[t]==0)
        {
          tarjan_dfs(t);
          low[s]=min(low[s],low[t]);
        }
        else
        {
          if (sccno[t]==0)
            low[s]=min(low[s],pre[t]);
        }
      }
    
      if (pre[s]==low[s])
      {
        scc_cnt++;
        for (;;)
        {
          int x=st[top--];
          sccno[x]=scc_cnt;
          w[scc_cnt]++;
          if (x==s) break;
        }
      }
    }
    
    void find_scc()
    {
      top=-1;
      scc_cnt=dfs_clock=0;
      memset(pre,0,sizeof pre);
      memset(low,0,sizeof low);
      memset(w,0,sizeof w);
      for (int i=1;i<=n;i++)
        if (pre[i]==0) tarjan_dfs(i);
    }
    
    int main()
    {
    #ifdef FCBRUCE
      freopen("/home/fcbruce/code/t","r",stdin);
    #endif // FCBRUCE
    
      scanf("%d%d",&n,&m);
    
      e_max=0;
      memset(fir,-1,sizeof fir);
    
      for (int e=0,u,v;e<m;e++)
      {
        scanf("%d%d",&u,&v);
        add_edge(u,v);
      }
    
      find_scc();
    
      memset(deg,0,sizeof deg);
    
      for (int e=0;e<e_max;e++)
      {
        if (sccno[u[e]]==sccno[v[e]]) continue;
        deg[sccno[u[e]]]++;
      }
    
      int cnt=0,the_one;
    
      for (int i=1;i<=scc_cnt;i++)
      {
        if (deg[i]==0)
        {
          cnt++;
          the_one=i;
        }
      }
    
      if (cnt==1) printf("%d
    ",w[the_one]);
      else  puts("0");
    
    
      return 0;
    }
    


  • 相关阅读:
    Spring Security -- 添加图形验证码(转载)
    Spring Security -- 自定义用户认证(转载)
    pandas agg 后降低df表层
    判断是否有人跟踪车辆的方案
    格隆汇笔记-黄勇演讲
    Mac mysql 忘记root密码的解决方法
    sql 同步2个表中的一个字段数据
    Linux下配置用msmtp和mutt发邮件
    spark StructType的应用,用在处理mongoDB keyvalue
    Idea2018旗舰版破解方法
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/5179213.html
Copyright © 2011-2022 走看看