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 <= 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两只奶牛没有跳成功的圆舞
Source
#include<cstdio>
#include<iostream>
#define N 10010
#define M 50010
using namespace std;
inline int min(int a,int b){return a<b?a:b;}
inline int read()
{
int x=;char ch=getchar();
while(ch<''||ch>'') ch=getchar();
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x;
}
int dfn[N],low[N],tp,a,b,n,m,lj[N],to[M],fro[M],cnt,q[N],tq,ans;
void add(int u,int v){cnt++;to[cnt]=v;fro[cnt]=lj[u];lj[u]=cnt;}
bool vs[N];
void tj(int k)
{
dfn[k]=low[k]=++tp;
q[++tq]=k;
vs[k]=;
for(int i=lj[k];i;i=fro[i])
{
if(!dfn[to[i]])
{
tj(to[i]);
low[k]=min(low[k],low[to[i]]);
}
else if(vs[to[i]]) low[k]=min(low[k],low[to[i]]);
}
if(dfn[k]==low[k])
{
int d=;
while(q[tq]!=k)
{
vs[q[tq--]]=;
d++;
}
vs[q[tq--]]=;
d++;
if(d>) ans++;
}
}
int main()
{
n=read(); m=read();
for(int i=;i<m;i++)
{
a=read();b=read();
add(a,b);
}
for(int i=;i<=n;i++) if(!vs[i]) tj(i);
printf("%d\n",ans);
}
bzoj 1654: [Usaco2006 Jan]The Cow Prom 奶牛舞会 -- Tarjan的更多相关文章
- 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 ...
- 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 1718: [Usaco2006 Jan] Redundant Paths 分离的路径( tarjan )
tarjan求边双连通分量, 然后就是一棵树了, 可以各种乱搞... ----------------------------------------------------------------- ...
- 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 ...
随机推荐
- lintcode 66.67.68 二叉树遍历(前序、中序、后序)
AC代码: /** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode le ...
- spring boot 加载原理
spring boot quick start 在springBoot里面,很吸引的一个特征就是可以直接把应用打包成jar/war包形式.然后jar/war包可以直接运行的.不需要再配置web Ser ...
- 南邮PHP反序列化
题目如下: <?php class just4fun { var $enter; var $secret; } if (isset($_GET['pass'])) { $pass = $_GET ...
- [005] unique_sub_string
[Description] Given a string, find the largest unique substring. e.g. str[] = "asdfghjkkjhgf&qu ...
- css预处理scss环境配置
css 预处理器 CSS 预处理器用一种专门的编程语言,进行 Web css编码,然后再编译成正常的 CSS 文件,以供项目使用:说简单点就是在某个环境下写css 可以写变量.表达式.嵌套等,在通过该 ...
- php pdo封装类
class MYPDO { protected static $_instance = null; protected $dbname = ''; protected $dsn; protected ...
- [转载]DirectX SDK (June 2010)安装错误S1023,解决方法
导致这个错误的原因是在安装DirectX SDK (June 2010)之前.我就安装了VS2010 . 所以也就安装了: Microsoft Visual C++ 2010 x86 Redistri ...
- 【转】Android打印机--没有设备驱动sdk,自己实现USB打印功能
原文:http://blog.csdn.net/johnwcheung/article/details/71576833 Android下的设备调试,如果设备提供了驱动,按照厂家的驱动调试即可:设备未 ...
- 2019.2.25 模拟赛T1【集训队作业2018】小Z的礼物
T1: [集训队作业2018]小Z的礼物 我们发现我们要求的是覆盖所有集合里的元素的期望时间. 设\(t_{i,j}\)表示第一次覆盖第i行第j列的格子的时间,我们要求的是\(max\{ALL\}\) ...
- python开发学习-day07(面向对象之多态、类的方法、反射、新式类and旧式类、socket编程)
s12-20160227-day07 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: ...