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
  • 相关阅读:
    Spring Boot 2 (四):使用 Docker 部署 Spring Boot
    Spring Boot 2 (三):Spring Boot 2 相关开源软件
    ConnectionString属性(网速慢的情况下研究Connect Timeout)
    Flexbox布局入门笔记
    ASP.Net一般处理程序Session用法
    AngularJS 插值字符串 $interpolate
    浅谈AngularJS的$parse服务
    VsCode 解决vim插件,不能使用ctrl+c的问题(目标卸载im)
    win10远程桌面 CredSSP加密Oracle修正的解决办法
    fatal:'origin' does not appear to be a git repository fatal:Could not read from remote repository
  • 原文地址:https://www.cnblogs.com/hyxsolitude/p/3697048.html
Copyright © 2011-2022 走看看