zoukankan      html  css  js  c++  java
  • POJ 2186

    Popular Cows
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 22189   Accepted: 9076

    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

     
    强连通分量缩点,然后拓扑序最大的强连通分量反向搜索,看能不能遍历到所有的结点。
      1 #include <iostream>
      2 #include <cstdio>
      3 #include <cstring>
      4 #include <algorithm>
      5 #include <stack>
      6 
      7 using namespace std;
      8 
      9 int N,M;
     10 const int MAX_N = 10005;
     11 const int edge = 50005;
     12 stack<int> S;
     13 int pre[MAX_N],low[MAX_N],cmp[MAX_N];
     14 int first[MAX_N],Next[edge],v[edge];
     15 int rfirst[MAX_N],rNext[edge],rv[edge];
     16 int dfs_clock,scc_cnt;
     17 
     18 void dfs(int u) {
     19         low[u] = pre[u] = ++dfs_clock;
     20         S.push(u);
     21         for(int e = first[u]; e != -1; e = Next[e]) {
     22                 if(!pre[ v[e] ]) {
     23                         dfs(v[e]);
     24                         low[u] = min(low[u],low[ v[e] ]);
     25                 } else if(!cmp[ v[e] ]) {
     26                         low[u] = min(low[u],pre[ v[e] ]);
     27                 }
     28         }
     29 
     30         if(low[u] == pre[u]) {
     31                 ++scc_cnt;
     32                 for(;;) {
     33                         int x = S.top(); S.pop();
     34                         cmp[x] = scc_cnt;
     35                         if(x == u) break;
     36                 }
     37         }
     38 }
     39 
     40 void scc() {
     41         dfs_clock = scc_cnt = 0;
     42         memset(cmp,0,sizeof(cmp));
     43         memset(pre,0,sizeof(pre));
     44         for(int i = 1; i <= N; ++i) {
     45                 if(!pre[i]) dfs(i);
     46         }
     47 }
     48 
     49 void dfs1(int u) {
     50         pre[u] = 1;
     51         for(int e = rfirst[u]; e != -1; e = rNext[e]) {
     52                 if(!pre[  rv[e] ]) {
     53                         dfs1(rv[e]);
     54                 }
     55         }
     56 }
     57 
     58 
     59 
     60 int solve() {
     61         scc();
     62         memset(pre,0,sizeof(pre));
     63         int u,ans = 0;
     64         for(int i = 1; i <= N; ++i)
     65                 if(cmp[i] == 1) {
     66                         u = i;
     67                         ++ans;
     68                 }
     69 
     70         dfs1(u);
     71         for(int i = 1; i <= N; ++i)  {
     72                 if(!pre[i]) return 0;
     73         }
     74         return ans;
     75 }
     76 
     77 void add_edge(int id,int u) {
     78         int e = first[u];
     79         Next[id] = e;
     80         first[u] = id;
     81 }
     82 
     83 void radd_edge(int id,int u) {
     84         int e = rfirst[u];
     85         rNext[id] = e;
     86         rfirst[u] = id;
     87 }
     88 int main()
     89 {
     90   //  freopen("sw.in","r",stdin);
     91     scanf("%d%d",&N,&M);
     92     for(int i = 1; i <= N; ++i) rfirst[i] = first[i] = -1;
     93     for(int i = 0; i < M; ++i) {
     94             int u;
     95             scanf("%d%d",&u,&v[i]);
     96             rv[i] = u;
     97             add_edge(i,u);
     98             radd_edge(i,v[i]);
     99     }
    100 
    101     printf("%d
    ",solve());
    102     return 0;
    103 }
    View Code
  • 相关阅读:
    jmeter压测:failed (99: Cannot assign requested address) while connecting to upstream,问题解决
    linux主机设置免密登录
    linux环境 jdk+mysql+redis安装初始化步骤
    互联网系统设计原则
    LINUX运维常用命令
    性能测试岗位常见面试题
    查看电脑已连接过的WIFI密码
    Jenkins安装后,安装插件失败。报错SunCertPathBuilderException
    【java】javac命令在win10不可用,提示javac不是内部或外部命令,也不是可运行的程序【解决方法】
    .NET跨平台实践:.NetCore、.Net5/6 Linux守护进程设计
  • 原文地址:https://www.cnblogs.com/hyxsolitude/p/3697048.html
Copyright © 2011-2022 走看看