zoukankan      html  css  js  c++  java
  • 18.11.08 POJ 2186 Popular Cows(有向图+缩点)

    描述

    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.
    输入

    * 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.
    输出* Line 1: A single integer that is the number of cows who are considered popular by every other cow.
    样例输入

    3 3
    1 2
    2 1
    2 3
    

    样例输出

    1
    

    提示

    Cow 3 is the only cow of high popularity.
    来源

    USACO 2003 Fall

     1 #include <iostream>
     2 #include <string.h>
     3 #include <algorithm>
     4 #include <stack>
     5 #include <string>
     6 #include <math.h>
     7 #include <queue>
     8 #include <stdio.h>
     9 #include <string.h>
    10 #include <set>
    11 #include <vector>
    12 #define maxn 10005
    13 #define inf 999999
    14 #define EPS 1e-10
    15 #define lowbit(x) (int)(x&(-x))
    16 using namespace std;
    17 
    18 int n, m, tm = 1, colorid;
    19 int dfn[maxn], low[maxn], color[maxn];
    20 bool vis[maxn], to[maxn];
    21 vector<vector<int>>G(maxn);
    22 stack<int>q;
    23 
    24 void tarjan(int u) {
    25     vis[u] = true;
    26     q.push(u);
    27     low[u]=dfn[u] = tm++;
    28     int size = G[u].size();
    29     for (int i = 0; i < size; i++) {
    30         int v = G[u][i];
    31         if (!dfn[v]) {
    32             tarjan(v);
    33             low[u] = min(low[u], low[v]);
    34         }
    35         else if (vis[v])
    36             low[u] = min(low[u], dfn[v]);
    37     }
    38     if (low[u] == dfn[u]) {
    39         int tmp = 0;
    40         ++colorid;
    41         while (tmp != u) {
    42             tmp = q.top(); q.pop();
    43             color[tmp] = colorid;
    44             vis[tmp] = false;
    45         }
    46     }
    47 }
    48 
    49 void solve() {
    50     for (int i = 1; i <= n; i++) {
    51         if (to[color[i]])continue;
    52         int size = G[i].size();
    53         for(int j=0;j<size;j++)
    54             if (color[i] != color[G[i][j]]) {
    55                 to[color[i]] = true;
    56                 break;
    57             }
    58     }
    59     int cnt=0, ans;
    60     for(int i=1;i<=colorid;i++)
    61         if (!to[i]) {
    62             cnt++;
    63             ans = i;
    64         }
    65     if (cnt != 1) {
    66         printf("0
    ");
    67         return;
    68     }
    69     cnt = 0;
    70     for (int i = 1; i <= n; i++)
    71         if (color[i] == ans)
    72             cnt++;
    73     printf("%d
    ", cnt);
    74 }
    75 
    76 void init() {
    77     scanf("%d%d", &n, &m);
    78     while (m--) {
    79         int x, y;
    80         scanf("%d%d", &x, &y);
    81         G[x].push_back(y);
    82     }
    83     for(int i=1;i<=n;i++)
    84         if(!dfn[i])
    85             tarjan(i);
    86     solve();
    87 }
    88 
    89 int main() {
    90     init();
    91     return 0;
    92 }
    View Code
    注定失败的战争,也要拼尽全力去打赢它; 就算输,也要输得足够漂亮。
  • 相关阅读:
    Android Studio基本使用
    convlstm学习资料
    Java重写与重载
    Java继承的缺点
    long转int
    解决RubyMine中puts中文显示乱码的问题
    Ruby入门1
    Ruby学习笔记
    matplotlib绘制饼状图
    python强大的绘图模块matplotlib示例讲解
  • 原文地址:https://www.cnblogs.com/yalphait/p/9927280.html
Copyright © 2011-2022 走看看