zoukankan      html  css  js  c++  java
  • POJ 2186 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. 

    题目大意:给n个点m条有向边,问有多少个点满足,所有其他点都有一条能到达它的路径。

    思路:先用tarjan求强联通分量,缩点,形成树。若有且只有一个点(缩了之后)出度为0,那么所有其他点都能到达它(缩点之后形成的是一个树状结构,出度为0的点为根)。若不止一个点出度为零,说明答案为0(因为这些出度为0的点之间不能互相到达)。

    代码(79MS):

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <iostream>
     4 #include <algorithm>
     5 using namespace std;
     6 
     7 const int MAXN = 10010;
     8 const int MAXE = 50010;
     9 
    10 int outdeg[MAXN], pre[MAXN], lowlink[MAXN], sum[MAXN];
    11 int head[MAXN], sccno[MAXN], ecnt, scc_cnt;
    12 int to[MAXE], next[MAXE];
    13 int n, m, dfs_clock;
    14 int stk[MAXN], top;
    15 
    16 void init() {
    17     memset(sum, 0, sizeof(sum));
    18     memset(head, 0, sizeof(head));
    19     memset(outdeg, 0, sizeof(outdeg));
    20     ecnt = 1;
    21     scc_cnt = 0;
    22     dfs_clock = 0;
    23 }
    24 
    25 void add_edge(int u, int v) {
    26     to[ecnt] = v; next[ecnt] = head[u]; head[u] = ecnt++;
    27 }
    28 
    29 void dfs(int u) {//tarjan
    30     pre[u] = lowlink[u] = ++dfs_clock;
    31     stk[++top] = u;
    32     for(int p = head[u]; p; p = next[p]) {
    33         int &v = to[p];
    34         if(!pre[v]) {
    35             dfs(v);
    36             if(lowlink[u] > lowlink[v]) lowlink[u] = lowlink[v];
    37         } else if(!sccno[v]) {
    38             if(lowlink[u] > pre[v]) lowlink[u] = pre[v];
    39         }
    40     }
    41     if(lowlink[u] == pre[u]) {
    42         ++scc_cnt;
    43         while(true) {
    44             int x = stk[top--];
    45             sccno[x] = scc_cnt;
    46             if(x == u) break;
    47         }
    48     }
    49 }
    50 
    51 int solve() {
    52     for(int i = 1; i <= n; ++i)
    53         if(!pre[i]) dfs(i);
    54     for(int u = 1; u <= n; ++u) {
    55         ++sum[sccno[u]];
    56         for(int p = head[u]; p; p = next[p]) {
    57             int &v = to[p];
    58             if(sccno[u] == sccno[v]) continue;
    59             ++outdeg[sccno[u]];
    60         }
    61     }
    62     int ans = 0;
    63     for(int i = 1; i <= scc_cnt; ++i)
    64         if(outdeg[i] == 0) {
    65             if(ans == 0) ans = sum[i];
    66             else return 0;
    67         }
    68     return ans;
    69 }
    70 
    71 int main() {
    72     scanf("%d%d", &n, &m);
    73     init();
    74     while(m--) {
    75         int a, b;
    76         scanf("%d%d", &a, &b);
    77         add_edge(a, b);
    78     }
    79     printf("%d
    ", solve());
    80 }
    View Code
  • 相关阅读:
    第五十四天:jQuery内容的基础:
    第五十三天dom基础
    第五十二天js的&#183;进阶
    第五十一天js的基础
    第四十九天css进阶
    第四十八天 html中的form和css基础
    第四十七天:web中德html初级:
    第42天IO模块
    第四十一天:协程操作
    第四十天线程的进阶
  • 原文地址:https://www.cnblogs.com/oyking/p/3299546.html
Copyright © 2011-2022 走看看