bzoj:1654 [Usaco2006 Jan]The Cow Prom 奶牛舞会
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
2 4
3 5
1 2
4 1
INPUT DETAILS:
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____/ /
\_______/
Sample Output
HINT
1,2,4这三只奶牛同属一个成功跳了圆舞的组合.而3,5两只奶牛没有跳成功的圆舞
表示并没有看懂题目……各种吐槽233,然后翻了翻了题解,想看看有没有题目大意,结果一打开就是“裸tarjan”,“强连通分量”,之类简洁明快的题解,呜呼,既然如此,我也写写看吧(其实以前没写过)。呵呵,居然A了,还是1A,2B青年欢乐多………
#include<cstdio>
#include<algorithm>
using namespace std; struct na{
int x,y,ne;
na(){
ne=;
}
};
int n,m,l[],r[],x,y,num=,ans=,top=,df[],lo[],dfn=,st[];
na b[];
bool pr[],inst[];
void in(int x,int y){
num++;
if (l[x]==) l[x]=num;else b[r[x]].ne=num;
b[num].x=x;b[num].y=y;r[x]=num;
}
void tarjan(int x){
pr[x]=;
df[x]=lo[x]=++dfn;
st[++top]=x;inst[x]=;
for (int i=l[x];i;i=b[i].ne){
if (!pr[b[i].y]){
tarjan(b[i].y);
lo[x]=min(lo[x],lo[b[i].y]);
}else if (inst[b[i].y]) lo[x]=min(lo[x],lo[b[i].y]);
}
if (df[x]==lo[x]){
int j,q=;
do{
j=st[top--];
inst[j]=;
q++;
}while(j!=x);
if (q>) ans++;
}
}
int main(){
scanf("%d%d",&n,&m);
for (int i=;i<=m;i++){
scanf("%d%d",&x,&y);
in(x,y);
}
for (int i=;i<=n;i++)
if (!pr[i]) tarjan(i);
printf("%d\n",ans);
}
bzoj:1654 [Usaco2006 Jan]The Cow Prom 奶牛舞会的更多相关文章
- bzoj 1654: [Usaco2006 Jan]The Cow Prom 奶牛舞会 -- Tarjan
1654: [Usaco2006 Jan]The Cow Prom 奶牛舞会 Time Limit: 5 Sec Memory Limit: 64 MB Description The N (2 & ...
- bzoj 1654: [Usaco2006 Jan]The Cow Prom 奶牛舞会【tarjan】
几乎是板子,求有几个size>1的scc 直接tarjan即可 #include<iostream> #include<cstdio> #include<cstri ...
- 【BZOJ】1654: [Usaco2006 Jan]The Cow Prom 奶牛舞会(tarjan)
http://www.lydsy.com/JudgeOnline/problem.php?id=1654 请不要被这句话误导..“ 如果两只成功跳圆舞的奶牛有绳索相连,那她们可以同属一个组合.” 这句 ...
- 【BZOJ1654】[Usaco2006 Jan]The Cow Prom 奶牛舞会 赤果果的tarjan
Description The N (2 <= N <= 10,000) cows are so excited: it's prom night! They are dressed in ...
- bzoj1654 [Usaco2006 Jan]The Cow Prom 奶牛舞会
Description The N (2 <= N <= 10,000) cows are so excited: it's prom night! They are dressed in ...
- 【强连通分量】Bzoj1654 [Usaco2006 Jan]The Cow Prom 奶牛舞会
Description 约翰的N(2≤N≤10000)只奶牛非常兴奋,因为这是舞会之夜!她们穿上礼服和新鞋子,别上鲜花,她们要表演圆舞. 只有奶牛才能表演这种圆舞.圆舞需要一些绳索和一个圆形的 ...
- P1654: [Usaco2006 Jan]The Cow Prom 奶牛舞会
裸的强连通 ; type node=record f,t:longint; end; var n,m,dgr,i,u,v,num,ans:longint; bfsdgr,low,head,f:arra ...
- BZOJ——1720: [Usaco2006 Jan]Corral the Cows 奶牛围栏
http://www.lydsy.com/JudgeOnline/problem.php?id=1720 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1 ...
- 【BZOJ1720】[Usaco2006 Jan]Corral the Cows 奶牛围栏 双指针法
[BZOJ1720][Usaco2006 Jan]Corral the Cows 奶牛围栏 Description Farmer John wishes to build a corral for h ...
随机推荐
- EntityFramework Core数据查询
前言 本节我们再来讲讲EF Core,本节算是回归基础吧,当前项目EF Core还是处于1.1版本中,后续等待.net core等版本稳定了全部会更新到2.0版本中,到时再来更新相关文章分享给大家. ...
- Git 企业开发者教程
为什么要写这样一个面向企业开发者的Git教程?这个问题也困扰我自己很久.其实我使用git的时间也不短了,但是就和正在阅读本文的每一位一样,常用的基本就是那么几个(git clone, git pu ...
- JavaScript简单入门(补充篇)
本文是对上一篇 JavaScript简单入门 的一些细节补充. 一.全局变量和局部变量 在<script>标签内定义的变量是当前页面中的全局变量.即 <script>标签可以直 ...
- Jmeter非GUI模式运行
非GUI模式,即命令行模式,运行 JMeter 测试脚本能够大大缩减所需要的系统资源. 使用的命令: jmeter -n -t 脚本文件路径 -l 结果输出文件路径 -j 日志文 ...
- 记录一个从没见过的bug
js的默认启动 $(function(){ )}; 不识别,意思是如果你把js内容放入这个东西里面,它不会执行.必须把它去掉才可以. 包括.tag里的文件也是一样. 这是发生在系统框架迁移发生的事,以 ...
- linux部署solr服务--小记
1.将solr压缩包上传到web项目-solr文件夹下 2.解压solr-5.5.4.zip到当前文件夹下 linux 解压zip文件到当前目录 unzip filename.zip 提示没有unzi ...
- 关于java字节流的read()方法返回值为int的思考
我们都知道java中io操作分为字节流和字符流,对于字节流,顾名思义是按字节的方式读取数据,所以我们常用字节流来读取二进制流(如图片,音乐 等文件).问题是为什么字节流中定义的read()方法返回值为 ...
- hiberation4 获取session
T t; Configuration cfg = new Configuration(); cfg.configure(); ServiceRegistry serviceRegistry = new ...
- C#保留2位小数的做法
第一 算法实现 保留两位的话,就用一个浮点型先乘以100,然后取整,取整完了之后,再乘以1.0,然后再除以100. 上面这种做法是保留n位,不会四舍五入的.因为这 ...
- primer漏配问题解决
在对之前的ITS数据(454数据)做split时,发现有一些reads没有被匹配上,但是barcode能够完全匹配,虽然之后的primer在中间漏了一个碱基,导致后面的碱基全部误匹配,从而导致这条re ...