zoukankan      html  css  js  c++  java
  • 洛谷P2863 [USACO06JAN]牛的舞会The Cow Prom

    ng 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 …

    输入输出格式

    输入格式:

    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.

    输出格式:

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

    输入输出样例

    输入样例#1: 复制
    5 4
    2 4
    3 5
    1 2
    4 1
    输出样例#1: 复制
    1

    说明

    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.

    强联通分量的裸题

    直接输出强联通分量的个数就好

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<stack>
     4 #include<queue>
     5 #include<map>
     6 #include<stack>
     7 #define ls k<<1
     8 #define rs k<<1|1
     9 #define LL long long 
    10 using namespace std;
    11 const int MAXN=100002;
    12 inline int read()
    13 {
    14     char c=getchar();int x=0,flag=1;
    15     while(c<'0'||c>'9')    {if(c=='-')    flag=-1;c=getchar();}
    16     while(c>='0'&&c<='9')    x=x*10+c-48,c=getchar();return x*flag;
    17 }
    18 struct node
    19 {
    20     int u,v,nxt;
    21 }edge[MAXN];
    22 int head[MAXN];
    23 int num=1;
    24 inline void add_edge(int x,int y)
    25 {
    26     edge[num].u=x;
    27     edge[num].v=y;
    28     edge[num].nxt=head[x];
    29     head[x]=num++;
    30 }
    31 int low[MAXN];
    32 int dfn[MAXN];//时间戳 
    33 int tot=0;
    34 stack<int>s;
    35 int ans=0;
    36 int vis[MAXN];
    37 int color[MAXN],colornum=0;
    38 int happen[MAXN];
    39 inline void Tarjan(int node)
    40 {
    41     low[node]=dfn[node]=++tot;
    42     vis[node]=1;
    43     s.push(node);
    44     for(int i=head[node];i!=-1;i=edge[i].nxt)
    45     {
    46         if(!dfn[edge[i].v])
    47             Tarjan(edge[i].v),low[node]=min(low[node],low[edge[i].v]);
    48         else if(vis[edge[i].v])
    49             low[node]=min(low[node],dfn[edge[i].v]);
    50     }
    51     if(dfn[node]==low[node])
    52     {
    53         colornum++;
    54         int h;
    55         do
    56         {    
    57             h=s.top();
    58             if(color[s.top()]==0)    color[s.top()]=colornum;
    59             vis[s.top()]=0;
    60             s.pop();
    61         }while(node!=h);
    62     }
    63 }
    64 int main()
    65 {
    66     memset(head,-1,sizeof(head));
    67     int n=read(),m=read();
    68     for(int i=1;i<=m;i++)
    69     {
    70         int x=read(),y=read();
    71         add_edge(x,y);
    72     }
    73     for(int i=1;i<=n;i++)    
    74         if(color[i]==0)
    75             Tarjan(i);
    76     for(int i=1;i<=n;i++)
    77         happen[color[i]]++;
    78     int ans=0;
    79     for(int i=1;i<=colornum;i++)
    80         if(happen[i]>1)
    81             ans++;
    82     printf("%d",ans);
    83     return 0;
    84 }
  • 相关阅读:
    每日英语:A Whiff Of 'Welcome Home'
    每日英语:What To Expect To Wear When You're Expecting
    每日英语:Success Outside the Dress Code
    每日英语:Mistrust Between U.S., Malaysia Strains Probe
    每日英语:A New Way to Learn Chinese
    真香,撸一个SpringBoot在线代码修改器
    SpringBoot代码生成器,从此不用手撸代码
    推荐一个能够让程序猿快速开发的极简工具箱
    臭名昭著的手机验证码功能是如何实现的
    SpringBoot 2.x 开发案例之前后端分离鉴权
  • 原文地址:https://www.cnblogs.com/zwfymqz/p/7723343.html
Copyright © 2011-2022 走看看