题目描述

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.

约翰的N (2 <= N <= 10,000)只奶牛非常兴奋,因为这是舞会之夜!她们穿上礼服和新鞋子,别 上鲜花,她们要表演圆舞.

只有奶牛才能表演这种圆舞.圆舞需要一些绳索和一个圆形的水池.奶牛们围在池边站好, 顺时针顺序由1到N编号.每只奶牛都面对水池,这样她就能看到其他的每一只奶牛.

为了跳这种圆舞,她们找了 M(2<M< 50000)条绳索.若干只奶牛的蹄上握着绳索的一端, 绳索沿顺时针方绕过水池,另一端则捆在另一些奶牛身上.这样,一些奶牛就可以牵引另一些奶 牛.有的奶牛可能握有很多绳索,也有的奶牛可能一条绳索都没有.

对于一只奶牛,比如说贝茜,她的圆舞跳得是否成功,可以这样检验:沿着她牵引的绳索, 找到她牵引的奶牛,再沿着这只奶牛牵引的绳索,又找到一只被牵引的奶牛,如此下去,若最终 能回到贝茜,则她的圆舞跳得成功,因为这一个环上的奶牛可以逆时针牵引而跳起旋转的圆舞. 如果这样的检验无法完成,那她的圆舞是不成功的.

如果两只成功跳圆舞的奶牛有绳索相连,那她们可以同属一个组合.

给出每一条绳索的描述,请找出,成功跳了圆舞的奶牛有多少个组合?

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 …

输入输出格式

输入格式:

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.

题解:tarjan好难啊嘤嘤嘤……

#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
typedef long long ll;
using namespace std;
const int N=10001,M=50001;
int pre[N],low[N],fi[N],nscc[N],isc[N],ne[M],b[M],clk,n,m,x,y,ans;
bool ri;
void dfs(int x){pre[x]=low[x]=++clk;for (int j=fi[x],y=b[j]; j; j=ne[j],y=b[j]){if (!pre[y]) dfs(y);if (!isc[y]) low[x]=min(low[x],low[y]);}if (pre[x]!=low[x]) ri=1;
}
int main(){freopen("2863.in","r",stdin);freopen("2863.out","w",stdout);scanf("%d%d",&n,&m);while (m){scanf("%d%d",&x,&y);b[m]=y; ne[m]=fi[x]; fi[x]=m--;}for (int i=1; i<=n; i++) if (!pre[i]){ri=0;dfs(i);if (ri) ++ans;}        printf("%d",ans);
}

转载于:https://www.cnblogs.com/wuhu-JJJ/p/11149426.html

【洛谷 2863】牛的舞会相关推荐

  1. python刷题 NOI题库 python题解 洛谷、牛客网、AcWing 刷题等

    NOI题库 python题解-2022.01.07整理(1.1-1.3) NOI题库 python题解-2022.01.07整理(1.1-1.3)_dllglvzhenfeng的博客-CSDN博客 N ...

  2. 洛谷1522 牛的旅行

    洛谷1522 牛的旅行 本题地址: http://www.luogu.org/problem/show?pid=1522 题目描述 农民 John的农场里有很多牧区.有的路径连接一些特定的牧区.一片所 ...

  3. 洛谷、牛客网、AcWing 刷题(python版)

    牛客网python专项练习整理(一) https://blog.csdn.net/weixin_41913008/article/details/87203468 牛客网剑指offer--python ...

  4. 洛谷or牛客数据结构+算法

    栈思想:先进后出 tips:栈里能放下标就放下标 (牛客)小c的计事本(直接用stack可以简化代码,且不会被自己绕晕,当时没意识到) (牛客)吐泡泡(没意识到用栈),(牛客)好串 1.后缀表达式(栈 ...

  5. USACO 06JAN 牛的舞会 洛谷2863

    题目描述 The N (2 <= N <= 10,000) cows are so excited: it's prom night! They are dressed in their ...

  6. 【洛谷P1538】迎春舞会之数字舞蹈

    迎春舞会之数字舞蹈 题目链接 一道奇怪的模拟题, 先将样例输出复制下来,观察观察 -- -- -- -- -- -- -- -- ①| | | | | | | | | | | | | | ②| | | ...

  7. 洛谷 P1477 [NOI2008]假面舞会

    题目链接 题目描述 一年一度的假面舞会又开始了,栋栋也兴致勃勃的参加了今年的舞会. 今年的面具都是主办方特别定制的.每个参加舞会的人都可以在入场时选择一 个自己喜欢的面具.每个面具都有一个编号,主办方 ...

  8. 洛谷-P1843-奶牛晒衣服

    原题: 熊大妈决定给每个牛宝宝都穿上可爱的婴儿装 . 于是 , 为牛宝宝洗晒衣服就成了很不爽的事情. 题目描述 熊大妈请你帮助完成这个重任 . 洗完衣服后 , 你就要弄干衣服 . 衣服在自然条件下用 ...

  9. 洛谷 P1522 牛的旅行 Cow Tours

    题目:牛的旅行 思路: 先预处理出两点间的距离,跑一边floyd,然后处理出每个点到离它最远的和它连通的距离L[i]. 然后再对于每个点,枚举所有和它不连通的点j,用L[i]+L[j]+d(i,j)更 ...

最新文章

  1. Linux_文件系统、磁盘分区_RHEL7
  2. Texmaker使用方法、Latex的优缺点
  3. linux 进程间通信 dbus-glib【实例】详解二(上) 消息和消息总线(附代码)
  4. 【原创翻译】生动详细解释javascript的冒泡和捕获,包懂包会
  5. 大数据可视化有哪些分析步骤
  6. demo 网络运维_【运维面试】面试官:你们公司的docker主要用来做啥?
  7. 预训练语言模型关系图+必读论文列表,清华荣誉出品
  8. TC Games无需安卓模拟器电脑玩绝地求生手游,按键设置详细教程
  9. 在计算机领域黑箱,探究黑箱的认知革命
  10. 基于物品的协同过滤算法实现图书推荐系统
  11. 一文看懂,Scrapy 底层逻辑是如此的简单
  12. Python Web 框架-Django day06
  13. mybatis 标签中prefix,suffix,prefixOverrides,suffixOverrides作用
  14. PAT甲级 A1031
  15. 匿名者Anonymous 十项大事记
  16. 计算机一级考试的时候用计算器,在WPS文字里设置计算器功能
  17. RAID 磁盘阵列与阵列卡
  18. 谁还不是一个宝宝了~
  19. 用上这门黑科技,运维从此不背锅!
  20. brew下载的mysql卸载_【已解决】Mac中如何彻底卸载之前通过dmg安装的mysql

热门文章

  1. java接口多态的变量能_「JAVA」多态的灵魂,面向接口的程序设计,这才是你该懂得的接口(interface)...
  2. python列表方法图解_python中list(列表)的使用方法总结(图文)
  3. his开发前途如何_八线城市开发人员前途迷思2019年开源年会后记
  4. jsp mysql 乱码_jsp插入mysql数据库后乱码的解决办法
  5. std::list 循环删除指针_数据结构_006_线性表_循环链表
  6. 嵌入式Linux结课报告,嵌入式课程报告.doc
  7. python colorama_Python常用模块—— Colorama模块
  8. memmove、memcpy和memccpy简介
  9. 光纤接口怎么接 图解_视频光端机怎么连接,视频光端机接法图解
  10. 无法为jsp编译类_《刺激战场》改名为《和平精英》,有2类玩家或可能无法登陆游戏...