zoukankan      html  css  js  c++  java
  • POJ2186 Popular Cows (强连通缩点)

    Popular Cows
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 18813   Accepted: 7570

    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

     

    思路:

      强连通缩点之后统计出度为0的节点是否唯一,若唯一则有解,统计该节点中真正包含的节点个数几位所求。

    我把POJ中讨论区中提供的数据都通过了,一直WA,不知道哪错了。。。但是基本算法都是对的。

    // OJ 测试 WA 代码

      1 #include <cstdio>
      2 #include <iostream>
      3 #include <stack>
      4 
      5 using namespace std;
      6 
      7 const int MAX = 10005;
      8 int n, m, cnt, order;
      9 int dfn[MAX], low[MAX], out[MAX], belong[MAX];
     10 bool instack[MAX];
     11 stack<int> S;
     12 
     13 struct edge
     14 {
     15     int num;
     16     struct edge *next;
     17 }head[MAX];
     18 
     19 int min(int a, int b)
     20 {
     21     return a < b ? a : b;
     22 }
     23 
     24 void init()
     25 {
     26     for(int i = 1; i <= n; ++i)
     27         head[i].next = NULL;
     28     memset(low, 0, sizeof(low));
     29     memset(dfn, 0, sizeof(dfn));
     30     memset(out, 0, sizeof(out));
     31     memset(instack, false, sizeof(instack));
     32 
     33     while(!S.empty())
     34         S.pop();
     35     cnt = 0;   // 记录强连通分量的个数
     36     order = 0;  //  时间戳
     37 }
     38 
     39 void targan(int u)
     40 {
     41     int v;
     42     dfn[u] = low[u] = order++;
     43     instack[u] =true;
     44     S.push(u);
     45     edge *t = head[u].next;
     46     for(; t != NULL; t = t->next)
     47     {
     48         if(!dfn[t->num])
     49         {
     50             targan(t->num);
     51             low[u] = min(low[u], low[t->num]);
     52         }
     53         else if(instack[t->num])
     54             low[u] = min(low[u], dfn[t->num]);
     55     }
     56     if(dfn[u] == low[u])
     57     {
     58         ++cnt;    // cnt 记录强连通分量的个数
     59         do
     60         {
     61             v = S.top();
     62             S.pop();
     63             instack[v] = false;
     64             belong[v] = cnt;
     65         }while(v != u);
     66     }
     67 }
     68 
     69 int solve()
     70 {
     71     edge *t;
     72     for(int i = 1; i <= n; ++i)
     73     {
     74         t = head[i].next;
     75         for(; t != NULL; t = t->next)
     76         {
     77             if(belong[i] != belong[t->num])
     78             {
     79                 ++out[belong[i]];
     80             }
     81         }
     82     }
     83     int tmp = 0;
     84     int flag;
     85     for(int i = 1; i <= cnt; ++i)
     86     {
     87         if(out[i] == 0)
     88         {
     89             ++tmp;
     90             flag = i;
     91         }
     92     }
     93     if(tmp > 1)  //  缩点后出度为0的节点有多个时候,则不连通,即不存在最受欢迎的牛
     94         return 0;
     95     int ans = 0;
     96     for(int j = 1; j <= n; ++j)//统计唯一的出度为0的节点中真正节点的个数
     97         if(belong[j] == flag)
     98             ++ans;
     99     return ans;
    100 }
    101 
    102 int main()
    103 {
    104     int a, b;
    105     edge *tmp;
    106     while(~scanf("%d%d", &n, &m))
    107     {
    108         init();
    109         while(m--)
    110         {
    111             scanf("%d%d", &a, &b);
    112             tmp = new edge;
    113             tmp->num = b;
    114             tmp->next = head[a].next;
    115             head[a].next = tmp;
    116         }
    117         for(int i = 1; i <= n; ++i)
    118         {
    119             if(!dfn[i])
    120                 targan(i);
    121         }
    122         printf("%d\n", solve());
    123     }
    124     return 0;
    125 }
  • 相关阅读:
    在Electron中最快速预加载脚本
    Babel是什么?
    node、npm、chrome、v8、sandbox是什么?
    我的博客即将入驻“云栖社区”,诚邀技术同仁一同入驻。
    NN[神经网络]中embedding的dense和sparse是什么意思?
    记一次失败的docker排障经历
    paddlepaddle关于使用dropout小案例
    paddlepaddle如何预加载embedding向量
    最近在部署推荐系统环境时,遇到一个很奇葩都问题
    fluid.io.load_inference_model 载入多个模型的时候会报错 -- [paddlepaddle]
  • 原文地址:https://www.cnblogs.com/dongsheng/p/3027711.html
Copyright © 2011-2022 走看看