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

    Sample Input

    3 3
    1 2
    2 1
    2 3
    

    Sample Output

    1
    

    Hint

    Cow 3 is the only cow of high popularity. 
     
     
    每一头牛的愿望就是变成一头最受欢迎的牛。现在有N头牛,给你M对整数(A,B),表示牛A认为牛B受欢迎。 这
    种关系是具有传递性的,如果A认为B受欢迎,B认为C受欢迎,那么牛A也认为牛C受欢迎。你的任务是求出有多少头
    牛被所有的牛认为是受欢迎的。
     
          先用tarjan求出每个强连通分量,再缩点,统计每个点的出度,如果有且只有1个出度为0的点,就输出这个点包含的节点数,否则输出0.
     1 #include<iostream>
     2 #include<algorithm>
     3 #include<vector>
     4 #include<stdio.h>
     5 #include<stdlib.h>
     6 #include<string.h>
     7 #define MaxNum 10005
     8 using namespace std;
     9  
    10 int n,m;
    11 vector<int > edge[MaxNum];
    12 int dfn[MaxNum],low[MaxNum];
    13 int time=0;
    14 int color=0;
    15 int allColor[MaxNum]={0};
    16 int allSum[MaxNum]={0};
    17 int stack[MaxNum],top=0;
    18 bool vis[MaxNum];
    19 
    20 void targan(int d){
    21     vis[d]=true;
    22     dfn[d]=++time;
    23     low[d]=time;
    24     stack[++top]=d;
    25     for(int i=0;i<edge[d].size();++i)
    26     {
    27         if(!vis[edge[d][i]])
    28         {
    29             targan(edge[d][i]);
    30             low[d]=min(low[d],low[edge[d][i]]);
    31         }
    32         else low[d]=min(low[d],dfn[edge[d][i]]);
    33     }
    34     
    35     if(dfn[d]==low[d])
    36     {
    37         ++color;
    38         while(stack[top]!=d)
    39         {
    40             allColor[stack[top--]]=color;
    41             allSum[color]++;
    42         }
    43         
    44         allColor[stack[top--]]=color;
    45         allSum[color]++;
    46     }
    47 }
    48 
    49 int out[MaxNum]={0};
    50 int main()
    51 {
    52     cin>>n>>m;
    53     for(int i=1;i<=m;++i)
    54     {
    55         int x,y;
    56         cin>>x>>y;
    57         edge[x].push_back(y);
    58     }
    59     
    60     memset(vis,false,sizeof(vis));
    61     for(int i=1;i<=n;++i)
    62     if(!vis[i])
    63     targan(i);
    64     
    65     
    66     for(int i=1;i<=n;++i)
    67     for(int j=0;j<edge[i].size();++j)
    68     if(allColor[i]!=allColor[edge[i][j]])
    69     out[allColor[i]]++;
    70     
    71     int total=0;int ans=0;
    72     for(int i=1;i<=color;++i)
    73     if(out[i]==0) 
    74     {
    75         total++;
    76         ans=allSum[i];
    77     }
    78     
    79     if(total>1) ans=0;
    80     cout<<ans<<endl;
    81     return 0;
    82 
    83 } 
  • 相关阅读:
    Intent属性详解
    LIBGDX游戏引擎平台介绍与搭建
    android教程之intent对象
    android教程之日期时间控件DatePicker/TimePicker
    DotNet Core 3.1 EF Core 数据库迁移(Migration)
    微服务介绍
    Asp.Net Core 认证授权:Cookie-based
    IdentityServer4 实现自定义 GrantType 授权模式
    SqlServer配置主从复制
    在【Stimulsoft-Reports-Net-2016.1】中使用DataSet做数据源新建报表
  • 原文地址:https://www.cnblogs.com/noip/p/9613980.html
Copyright © 2011-2022 走看看