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
    注定失败的战争,也要拼尽全力去打赢它; 就算输,也要输得足够漂亮。
  • 相关阅读:
    sql server 跟踪各事件的字段项编码及解释
    sql server 有关锁的视图说明 syslockinfo
    SQL Server:查看SQL日志文件大小命令:dbcc sqlperf(logspace)
    [SqlServer]创建链接服务器
    SQL Server 2008 存储过程,带事务的存储过程(创建存储过程,删除存储过程,修改存储过
    sql server 索引分析相关sql
    IO系统性能之一:衡量性能的几个指标
    Writing to a MySQL database from SSIS
    用漫画的形式来讲解为什么MySQL数据库要用B+树存储索引?
    一份 Tomcat 和 JVM 的性能调优经验总结!拿走不谢
  • 原文地址:https://www.cnblogs.com/yalphait/p/9927280.html
Copyright © 2011-2022 走看看