The Cow Prom POJ 3180

12 篇文章 0 订阅

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.

题意:N (2 <= N <= 10,000)头奶牛很兴奋:这是舞会之夜!他们穿着最漂亮的长袍,配着鲜花和新鞋。他们知道今晚每个人都将试着表演圆舞。只有奶牛可以表演圆形舞蹈,这需要一套绳子和一个圆形蓄水池。首先,奶牛排成一圈,从1..N.开始按顺时针顺序给自己编号。每头奶牛都面对着水池,这样她就能看到其他的舞者。然后他们总共获得M (2 <= M <= 50,000)根绳子,所有这些绳子都分发给用蹄子拴住它们的奶牛。每头牛都希望有一根或多根绳子拴在左右蹄上;一些奶牛可能会失望。对于任何一头奶牛(比如贝茜)来说,圆舞要想成功,她所持的绳子必须配置得恰到好处。要想知道贝西的舞蹈是否成功,我们必须看看一群牵着她绳子另一端的奶牛(如果她有的话),还有牵着绳子另一端的奶牛,等等。当贝西绕着水缸顺时针跳舞时,她必须立即把群里的其他奶牛也顺时针拉起来。同样地,如果她以另一种方式跳舞,她必须立即将整个组按逆时针方向拉(英式英语中为逆时针)。当然,如果绳子没有正确的分配,那么一群奶牛可能不会形成一个合适的舞蹈团体,因此不能成功的圆舞。发生这种情况的一种方式是,只有一根绳子连接两头奶牛。一头牛可以把另一头牛往一个方向拉,但不能把另一头牛往另一个方向拉(因为众所周知推绳子是徒劳的)。请注意,奶牛必须同步跳舞:一只悬挂的奶牛(可能只有一根绳子)最终被拉着向前走,由于它没有立即被拉到与其他奶牛步调一致的地方,所以它没有资格正确地表演圆形舞蹈。考虑到绳子和它们在奶牛身上的分布,有多少组奶牛可以正确地表演圆形舞?请注意,一套绳子和奶牛可能会绕着蓄水池很多次。输入行1:两个用空格分隔的整数:N和M行2..M+1:每条线包含两个用空格分隔的整数A和B,它们以顺时针方向描述一条从牛A到牛B的绳子。输出行1:带有单个整数的单行,该整数表示成功跳圆舞的组数。

思路:用tarjan缩点,求出包含点数大于1的强连通分量数量

#include <cstdio>
#include<iostream>
#include <cstring>
#include <algorithm>
#include <stack>
using namespace std;
#define ll long long
const int maxn=2e5;
stack<int>q;
int cnt,n,m,head[maxn],dfn[maxn],low[maxn],co[maxn],tot,id,out[maxn],a[maxn],ans[maxn];
struct Edge
{
    int to,next;
}e[maxn];
void add(int u,int v)
{
    e[cnt].to=v;
    e[cnt].next=head[u];
    head[u]=cnt++;
}
void init()
{
    memset(out,0,sizeof(out));
    memset(co,0,sizeof(co));
    memset(head,-1,sizeof(head));
    memset(dfn,0,sizeof(dfn));
    memset(low,0,sizeof(low));
    while(!q.empty()) q.pop();
    cnt=0;id=0,tot=0;
}
void tarjan(int u)
{
    q.push(u);
    dfn[u]=low[u]=++tot;
    for(int i=head[u];i!=-1;i=e[i].next)
    {
        int v=e[i].to;
        if(!dfn[v])
        {
            tarjan(v);
            low[u]=min(low[u],low[v]);
        }
        else if(!co[v])
            low[u]=min(low[u],dfn[v]);
    }
    if(low[u]==dfn[u])
    {
        id++;
        while(q.top()!=u)
        {
            int x=q.top();
            q.pop();
            co[x]=id;
        }
        co[u]=id;
        q.pop();
    }
}
int main()
{
    while(scanf("%d %d",&n,&m)!=EOF)
    {
        init();
        for(int i=1;i<=m;i++)
        {
            int u,v;
            scanf("%d %d",&u,&v);
            add(u,v);
        }
        for(int i=1;i<=n;i++)
        {
            if(dfn[i]==0)
            {
                tarjan(i);
            }
        }
        for(int i=1;i<=n;i++)
        {
            ans[co[i]]++;
        }
        int num=0;
        for(int i=1;i<=id;i++)
        {
            if(ans[i]>1) num++;
        }
        printf("%d\n",num);
    }
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值