zoukankan      html  css  js  c++  java
  • POJ 3180

    The Cow Prom
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 1132   Accepted: 713

    Description

    The N (2 <= N <= 10,000) cows are so excited: it's prom night! They are dressed in their finest gowns, complete with corsages and new shoes. They know that tonight they will each try to perform the Round Dance. 

    Only cows can perform the Round Dance which requires a set of ropes and a circular stock tank. To begin, the cows line up around a circular stock tank and number themselves in clockwise order consecutively from 1..N. Each cow faces the tank so she can see the other dancers. 

    They then acquire a total of M (2 <= M <= 50,000) ropes all of which are distributed to the cows who hold them in their hooves. Each cow hopes to be given one or more ropes to hold in both her left and right hooves; some cows might be disappointed. 

    For the Round Dance to succeed for any given cow (say, Bessie), the ropes that she holds must be configured just right. To know if Bessie's dance is successful, one must examine the set of cows holding the other ends of her ropes (if she has any), along with the cows holding the other ends of any ropes they hold, etc. When Bessie dances clockwise around the tank, she must instantly pull all the other cows in her group around clockwise, too. Likewise, 
    if she dances the other way, she must instantly pull the entire group counterclockwise (anti-clockwise in British English). 

    Of course, if the ropes are not properly distributed then a set of cows might not form a proper dance group and thus can not succeed at the Round Dance. One way this happens is when only one rope connects two cows. One cow could pull the other in one direction, but could not pull the other direction (since pushing ropes is well-known to be fruitless). Note that the cows must Dance in lock-step: a dangling cow (perhaps with just one rope) that is eventually pulled along disqualifies a group from properly performing the Round Dance since she is not immediately pulled into lockstep with the rest. 

    Given the ropes and their distribution to cows, how many groups of cows can properly perform the Round Dance? Note that a set of ropes and cows might wrap many times around the stock tank.

    Input

    Line 1: Two space-separated integers: N and M 

    Lines 2..M+1: Each line contains two space-separated integers A and B that describe a rope from cow A to cow B in the clockwise direction.

    Output

    Line 1: A single line with a single integer that is the number of groups successfully dancing the Round Dance.

    Sample Input

    5 4
    2 4
    3 5
    1 2
    4 1

    Sample Output

    1

    Hint

    Explanation of the sample: 

    ASCII art for Round Dancing is challenging. Nevertheless, here is a representation of the cows around the stock tank: 
           _1___
    
    /****
    5 /****** 2
    / /**TANK**|
    ********/
    ******/ 3
    4____/ /
    \_______/
    Cows 1, 2, and 4 are properly connected and form a complete Round Dance group. Cows 3 and 5 don't have the second rope they'd need to be able to pull both ways, thus they can not properly perform the Round Dance.

    Source

     
    强连通分量
     
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 #include <stack>
     6 
     7 using namespace std;
     8 
     9 const int MAX_N = 10005;
    10 const int edge = 50005;
    11 int N,M;
    12 int first[MAX_N],Next[edge],v[edge];
    13 int low[MAX_N],pre[MAX_N],cmp[MAX_N];
    14 int num[MAX_N];
    15 int dfs_clock,scc_cnt;
    16 stack<int> S;
    17 
    18 
    19 void add_edge(int id,int u) {
    20         int e = first[u];
    21         Next[id] = e;
    22         first[u] = id;
    23 }
    24 
    25 void dfs(int u) {
    26         pre[u] = low[u] = ++dfs_clock;
    27         S.push(u);
    28         for(int e = first[u]; e != -1; e = Next[e]) {
    29                 if(!pre[ v[e] ]) {
    30                         dfs( v[e] );
    31                         low[u] = min(low[u],low[ v[e] ]);
    32                 } else if(!cmp[ v[e] ]) {
    33                         low[u] = min(low[u],pre[ v[e] ]);
    34                 }
    35         }
    36 
    37         if(low[u] == pre[u]) {
    38                 ++scc_cnt;
    39                 for(;;) {
    40                         int x = S.top(); S.pop();
    41                         cmp[x] = scc_cnt;
    42                         num[scc_cnt]++;
    43                         if(x == u) break;
    44                 }
    45         }
    46 }
    47 
    48 void scc() {
    49         dfs_clock = scc_cnt = 0;
    50         memset(cmp,0,sizeof(cmp));
    51         memset(pre,0,sizeof(pre));
    52 
    53         for(int i = 1; i <= N; ++i) if(!pre[i]) dfs(i);
    54 }
    55 
    56 void solve() {
    57         int ans = 0;
    58         scc();
    59         for(int i = 1; i <= scc_cnt; ++i) {
    60                 if(num[i] >= 2) ++ans;
    61         }
    62 
    63         printf("%d
    ",ans);
    64 }
    65 int main()
    66 {
    67     、、freopen("sw.in","r",stdin);
    68     scanf("%d%d",&N,&M);
    69     for(int i = 1; i <= N; ++i) first[i] = -1;
    70     for(int i = 0; i < M; ++i) {
    71             int u;
    72             scanf("%d%d",&u,&v[i]);
    73             add_edge(i,u);
    74     }
    75 
    76     solve();
    77     return 0;
    78 }
    View Code
  • 相关阅读:
    人生,别认输,因为没人希望你赢
    一张图看懂开源许可协议,开源许可证GPL、BSD、MIT、Mozilla、Apache和LGPL的区别
    Android Studio 出现 Gradle's dependency cache may be corrupt 解决方案
    清华梦的粉碎——转自王垠
    label smooth
    <现代C++实战30讲>笔记 01 | 堆、栈、RAII:C++里该如何管理资源?
    h5转pb的两个坑
    opencv库的像素x,y,width,height,col,row的对应关系
    detect 导图
    keras多gpu训练
  • 原文地址:https://www.cnblogs.com/hyxsolitude/p/3697073.html
Copyright © 2011-2022 走看看