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 ...
随机推荐
- Android学习总结(六)———— 发送自定义广播
一.两种广播类型 2.1 标准广播 是一种完全异步执行的广播,在广播发出去之后,所有的广播接收器几乎都会在同一时刻接收到这条广播消息,因此它们之间没有任何先后顺序可言.这种广播的效率会比较高,但同时也 ...
- codevs 1497 取余运算
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 输入b,p,k的值,编程计算bp mod k的值.其中的b,p,k*k为长 ...
- iOS 查看包架构信息
lipo -info libUMSocial_Sdk_4.2.a 查看包架构信息
- EF+linq的增删改查
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...
- PE基础2
PE课程002 怎么找到Nt头? (PIMAGE_NT_HEADER)(DOS.e_lfanew + (DWORD)m_pBuff) 怎么找到第一个区段表? 区段头位置 = pNt + 4 + 文件头的 ...
- Dance links算法
其实Dance links只是一种数据结构,Dance links 才是一种算法.dacing links x就是一个高效的求解该类问题的算法,而这种算法,基于交叉十字循环双向 链表.下面是双向十字链 ...
- js中声明函数的三种方式
己亥年 庚午月 癸巳日 宜入宅 忌婚嫁 函数声明方式 声明 : function first(){}: 调用:first() 函数表达式声明方式 声明: var second=function ...
- 线程调度的问题:Lock Convoy(锁封护)与Priority Inversion(优先级反转)
Lock Convoy(锁封护) [1]Lock Convoy是在多线程并发环境下由于锁的使用而引起的性能退化问题.当多个相同优先级的线程频繁地争抢同一个锁时可能会引起lock convoy问题,一般 ...
- 通过工厂模式批量创建对象后调用其中方法 出现XXXis not a function()问题原因
//通过工厂模式批量创建 function Computer(color,weight,logo){ var obj=new Object(); obj.color=c ...
- [LUOGU] 1364 医院设置
设有一棵二叉树,如图: [我是图] 其中,圈中的数字表示结点中居民的人口.圈边上数字表示结点编号,现在要求在某个结点上建立一个医院,使所有居民所走的路程之和为最小,同时约定,相邻接点之间的距离为1.如 ...