poj3180 The Cow Prom
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 2373 | Accepted: 1402 |
Description
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
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
Sample Input
5 4
2 4
3 5
1 2
4 1
Sample Output
1
Hint
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.
Source
#include <cstdio>
#include <stack>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; const int maxn = ; int n,m,head[maxn],to[maxn],nextt[maxn],tot = ,pre[maxn],low[maxn],scc[maxn],cnt,du[maxn],dfs_clock;
int ans,numm,num[maxn];
stack <int> s; void add(int x,int y)
{
to[tot] = y;
nextt[tot] = head[x];
head[x] = tot++;
} void tarjan(int u)
{
s.push(u);
pre[u] = low[u] = ++dfs_clock;
for (int i = head[u];i;i = nextt[i])
{
int v = to[i];
if (!pre[v])
{
tarjan(v);
low[u] = min(low[u],low[v]);
}
else
if (!scc[v])
low[u] = min(low[u],pre[v]);
}
if (pre[u] == low[u])
{
cnt++;
while()
{
int t = s.top();
s.pop();
scc[t] = cnt;
num[cnt]++;
if(t == u)
break;
}
}
} int main()
{
scanf("%d%d",&n,&m);
for(int i = ; i <= m; i++)
{
int a,b;
scanf("%d%d",&a,&b);
add(a,b);
}
for (int i = ; i <= n; i++)
if (!pre[i])
tarjan(i);
for (int i = ; i <= cnt; i++)
if (num[i] >= )
ans++;
printf("%d\n",ans); return ;
}
poj3180 The Cow Prom的更多相关文章
- poj 3180 The Cow Prom(强联通分量)
http://poj.org/problem?id=3180 The Cow Prom Time Limit: 1000MS Memory Limit: 65536K Total Submissi ...
- bzoj1654 / P2863 [USACO06JAN]牛的舞会The Cow Prom
P2863 [USACO06JAN]牛的舞会The Cow Prom 求点数$>1$的强连通分量数,裸的Tanjan模板. #include<iostream> #include&l ...
- 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 & ...
- P2863 [USACO06JAN]牛的舞会The Cow Prom
洛谷——P2863 [USACO06JAN]牛的舞会The Cow Prom 题目描述 The N (2 <= N <= 10,000) cows are so excited: it's ...
- luoguP2863 [USACO06JAN]牛的舞会The Cow Prom
P2863 [USACO06JAN]牛的舞会The Cow Prom 123通过 221提交 题目提供者 洛谷OnlineJudge 标签 USACO 2006 云端 难度 普及+/提高 时空限制 1 ...
- POJ3180:The Cow Prom——题解
http://poj.org/problem?id=3180 英文题以后都不粘贴题面. 大意:求点数大于1的强连通分量个数 #include<stack> #include<cstd ...
- 【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 ...
- poj 3180 The Cow Prom(tarjan+缩点 easy)
Description The N ( <= N <= ,) cows are so excited: it's prom night! They are dressed in their ...
- 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 ...
随机推荐
- 解决更新到os x10.11后openssl头文件无法找到的问题
os x从10.10更新到10.11后,原有代码编译报错,#include <openssl/ssl.h>等头文件无法找到: "openssl/ssl.h: No such fi ...
- C# for循环的嵌套 作用域
for() { 循环体可以套无数个for循环 } 比如:for() { for() { for() {... ...这里面可以镶嵌无数个for循环} } } 也可以这样 for() { for() ...
- RSA不对称加密和公钥 私钥
理论上只要有加密的规则 基本都是可以解密的 但是如果解密需要消耗的时间过长 比如1000年 解密过后已经没什么意义了 此时可认为这种算法不能被破解 也就是说此加密可信 MD5 是一种单向操作 加密后不 ...
- Android串口通信
前段时间因为工作需要研究了一下android的串口通信,网上有很多讲串口通信的文章,我在做的时候也参考了很多文章,现在就将我学习过程中的一些心得分享给大家,希望可以帮助大家在学习的时候少走一些弯路,有 ...
- 51nod 1412 AVL数的种类(DP
题意给了n个节点 问AVL树的种类 卧槽 真的好傻 又忘记这种题可以打表了 就算n^3 也可以接受的 树的深度不大 那么转移方程很明显了 dp[i][j] 代表的是节点为n深度为j的树的种类 k ...
- poj2104 K大数 划分树
题意:给定一个数列,求一个区间的第K大数 模板题, 其中的newl, newr 有点不明白. #include <iostream> #include <algorithm> ...
- OpenCV2:第八章 界面事件
一.简介 OpenCV中提供了程序界面中的鼠标和键盘事件 二.鼠标事件 // 设置鼠标回调函数 void setMouseCallback ( const string& winname, ...
- Django REST framework 中的视图
1.Request REST framework传入视图的request对象不再是Django默认的Httprequest对象,而是DRF提供的扩展类的Request类的对象 常用属性 request ...
- (28)zabbix用户宏变量详解macro
zabbix宏变量让zabbix变得更灵活,变量可以定义在主机.模板以及全局,变量名称类似:{$MACRO},宏变量都是大写的.认识了宏变量,你会感叹zabbix越发的强大. 变量可以用于如下地方: ...
- 转载:jquery.ajax之beforeSend方法使用介绍
常见的一种效果,在用ajax请求时,没有返回前会出现前出现一个转动的loading小图标或者“内容加载中..”,用来告知用户正在请求数据.这个就可以用beforeSend方法来实现. 下载demo:a ...