题目描述

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>
const int maxn=1e4+;
const int maxm=1e5+;
int n,m,ans;
int a,b;
int h[maxn],hs;
int e_s[maxm],e_t[maxm],e_n[maxm];
inline int min_(int x,int y){return x<y?x:y;}
int dn[maxn],fl[maxn],st[maxn],dns,top;
bool v[maxn];
int col[maxn],num[maxn],cs;
void tarjan(int k){
dn[k]=fl[k]=++dns;
st[++top]=k,v[k]=true;
for(int i=h[k];i;i=e_n[i]){
if(v[e_t[i]]) fl[k]=min_(fl[k],dn[e_t[i]]);
if(!dn[e_t[i]]){
tarjan(e_t[i]);
fl[k]=min_(fl[k],fl[e_t[i]]);
}
}
if(dn[k]==fl[k]){
++cs;
while(st[top+]!=k){
num[cs]++;
v[st[top]]=false;
col[st[top--]]=cs;
}
}
}
int main(){
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++){
scanf("%d%d",&a,&b);
++hs,e_s[hs]=a,e_t[hs]=b,e_n[hs]=h[a],h[a]=hs;
}
for(int i=;i<=n;i++) if(!dn[i]) tarjan(i);
for(int i=;i<=cs;i++) if(num[i]>) ans++;
printf("%d\n",ans);
return ;
}

[USACO06JAN]牛的舞会The Cow Prom Tarjan的更多相关文章

  1. luogu P2863 [USACO06JAN]牛的舞会The Cow Prom |Tarjan

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

  2. bzoj1654 / P2863 [USACO06JAN]牛的舞会The Cow Prom

    P2863 [USACO06JAN]牛的舞会The Cow Prom 求点数$>1$的强连通分量数,裸的Tanjan模板. #include<iostream> #include&l ...

  3. P2863 [USACO06JAN]牛的舞会The Cow Prom

    洛谷——P2863 [USACO06JAN]牛的舞会The Cow Prom 题目描述 The N (2 <= N <= 10,000) cows are so excited: it's ...

  4. luoguP2863 [USACO06JAN]牛的舞会The Cow Prom

    P2863 [USACO06JAN]牛的舞会The Cow Prom 123通过 221提交 题目提供者 洛谷OnlineJudge 标签 USACO 2006 云端 难度 普及+/提高 时空限制 1 ...

  5. 洛谷 P2863 [USACO06JAN]牛的舞会The Cow Prom(Tarjan)

    一道tarjan的模板水题 在这里还是着重解释一下tarjan的代码 #include<iostream> #include<cstdio> #include<algor ...

  6. [luoguP2863] [USACO06JAN]牛的舞会The Cow Prom(Tarjan)

    传送门 有向图,找点数大于1的强连通分量个数 ——代码 #include <stack> #include <cstdio> #include <cstring> ...

  7. [USACO06JAN] 牛的舞会 The Cow Prom

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

  8. 洛谷——P2863 [USACO06JAN]牛的舞会The Cow Prom

    https://www.luogu.org/problem/show?pid=2863#sub 题目描述 The N (2 <= N <= 10,000) cows are so exci ...

  9. [USACO06JAN]牛的舞会The Cow Prom

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

随机推荐

  1. knockout 和mvc4结合使用

    Knockout (或者Knockout.js ,KnockoutJS)是一个开源的JavaScript库,网址为www.knockoutjs.com.Knockout语法简洁.可读性好,能轻松实现与 ...

  2. 探寻宝藏 --- 双线DP

    双线DP , 在郑轻的时候 做过 这种双线DP  ,  这是多维DP 应该是比较简单的  但是那个 时间复杂度的优化 始终看不懂 .  先附上代码吧 , 等看懂了再来 , 补充一下 解释  . #in ...

  3. linux学习之路5 系统常用命令

    日期时间 查看设置当前时间 date +%Y--%m--%d 格式化显示时间 -s " "(切换到超级用户)修改时间 hwclock(clock)用以显示硬件时钟时间 命令 cal ...

  4. C# 相关概念

    解决方案 在磁盘上由 .sln 文件表示,是一个或多个相关项目的容器. 例如,如果为 Python 应用程序编写 C++ 扩展,该 C++ 项目可以驻留在同一解决方案中. 解决方案还可以包含 Web ...

  5. kafka的topic命名技巧

    不多说,直接上干货!    比如,我们给kafka的topic命名为user_r2p10 表示user这个topic的副本因子(r)是2,分区数(p)是10. 这样后期在写消费者代码的时候,根据top ...

  6. Xml学习笔记(3)利用递归解析Xml文档添加到TreeView中

    利用递归解析Xml文档添加到TreeView中 private void Form1_Load(object sender, EventArgs e) { XmlDocument doc = new ...

  7. [ CCO 2015 ] Artskjid

    \(\\\) \(Description\) \(N\)个点\(M\)条边的有向图,求从\(0\)号节点出发,\(N-1\)号节点结束,且图中每个点至多经过一次的最长路. \(N\in[2,18]\) ...

  8. 百度地图API在vue-cli中路径错误的问题

    在使用百度地图的时候,需要使用自定义的icon图片,百度的案例中使用的是线上地址,但当替换为本地图片路径的时候,错误出现了 这是本地图片地址 ) // 设置覆盖物大小 ); 这里有一点需要注意,这里路 ...

  9. RocketMQ学习笔记(13)----RocketMQ的Consumer消息重试

    1. 概念 Producer端重试: 生产者端的消息失败,也就是Producer往MQ上发消息没有发送成功,比如网络抖动导致生产者发送消息到MQ失败. 这种消息失败重试我们可以手动设置发送失败重试的次 ...

  10. 个人作业Alpha项目测试

    这个作业属于哪个课程 软件工程原理 这个作业要求在哪里 作业要求 团队名称 TEAMPANTHER 这个作业的目标 每个同学必须选取非自己所在团队的3个项目进行测试. 在你所测试的项目的Alpha发布 ...