poj 3180 The Cow Prom(tarjan+缩点 easy)
The N ( <= N <= ,) 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 ..N. Each cow faces the tank so she can see the other dancers. They then acquire a total of M ( <= M <= ,) 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 : Two space-separated integers: N and M Lines ..M+: 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 : A single line with a single integer that is the number of groups successfully dancing the Round Dance.
Sample Input
Sample Output
Hint
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**| \ \********/ \ \******/ \ 4____/ / \_______/
Cows , , and are properly connected and form a complete Round Dance group. Cows and 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<iostream>
#include<cstdio>
#include<cstring>
#include<stack>
#include<vector>
#include<map>
using namespace std;
#define N 100006
int n,m;
int tot; int head[N];
int vis[N];
int tt;
int scc;
stack<int>s;
int dfn[N],low[N];
int col[N]; struct Node
{
int from;
int to;
int next;
}edge[N<<];
void init()
{
tot=;
scc=;
tt=;
memset(head,-,sizeof(head));
memset(dfn,-,sizeof(dfn));
memset(low,,sizeof(low));
memset(vis,,sizeof(vis));
memset(col,,sizeof(col));
}
void add(int s,int u)//邻接矩阵函数
{
edge[tot].from=s;
edge[tot].to=u;
edge[tot].next=head[s];
head[s]=tot++;
}
void tarjan(int u)//tarjan算法找出图中的所有强连通分支
{
dfn[u] = low[u]= ++tt;
vis[u]=;
s.push(u);
int cnt=;
for(int i=head[u];i!=-;i=edge[i].next)
{
int v=edge[i].to;
if(dfn[v]==-)
{
// sum++;
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(vis[v]==)
low[u]=min(low[u],dfn[v]);
}
if(dfn[u]==low[u])
{
int x;
scc++;
do{
x=s.top();
s.pop();
col[x]=scc;
vis[x]=;
}while(x!=u);
}
}
int main()
{
while(scanf("%d%d",&n,&m)==)
{
init();
for(int i=;i<m;i++){
int x,y;
scanf("%d%d",&x,&y);
add(x,y);
} for(int i=;i<=n;i++)
{
if(dfn[i]==-)
{
tarjan(i);
}
}
//printf("%d\n",scc);
map<int,int> mp;
for(int i=;i<=n;i++){
mp[col[i]]++;
}
int ans=;
for(int i=;i<=scc;i++){
if(mp[i]>) ans++;
}
printf("%d\n",ans);
}
return ;
}
poj 3180 The Cow Prom(tarjan+缩点 easy)的更多相关文章
- POJ 3180 The cow Prom Tarjan基础题
题目用google翻译实在看不懂 其实题目意思如下 给一个有向图,求点个数大于1的强联通分量个数 #include<cstdio> #include<algorithm> #i ...
- poj 3180 The Cow Prom(强联通分量)
http://poj.org/problem?id=3180 The Cow Prom Time Limit: 1000MS Memory Limit: 65536K Total Submissi ...
- POJ 3180 The Cow Prom(SCC)
[题目链接] http://poj.org/problem?id=3180 [题目大意] N头牛,M条有向绳子,能组成几个歌舞团?要求顺时针逆时针都能带动舞团内所有牛. [题解] 等价于求点数大于1的 ...
- LuoGu-P2863牛的舞会The Cow Prom[tarjan 缩点模板]
传送门:https://www.luogu.org/problemnew/show/P2863 思路:tarjan模板题,之前会的tarjan,一直想学缩点到底是什么操作,发现就是把同组的放在一个数组 ...
- POJ 3180 The Cow Prom(强联通)
题目大意: 约翰的N(2≤N≤10000)只奶牛非常兴奋,因为这是舞会之夜!她们穿上礼服和新鞋子,别上鲜花,她们要表演圆舞. 只有奶牛才能表演这种圆舞.圆舞需要一些绳索和一个圆形的 ...
- [poj] 3180 the cow prom
原题 这是一道强连通分量板子题. 我们只用输出点数大于1的强连通分量的个数! #include<cstdio> #include<algorithm> #include< ...
- POJ 1236 Network of Schools Tarjan缩点
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22729 Accepted: 89 ...
- [USACO06JAN]牛的舞会The Cow Prom Tarjan
题目描述 The N (2 <= N <= 10,000) cows are so excited: it's prom night! They are dressed in their ...
- 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 ...
随机推荐
- UIScrollView使用autolayout 垂直滚动
转自:http://dadage456.blog.163.com/blog/static/30310744201491141752716 1.创建一个空白的UIViewController .将UIS ...
- JQuery的父、子、兄弟节点查找,节点的子节点循环
Query.parent(expr) //找父元素 jQuery.parents(expr) //找到所有祖先元素,不限于父元素 jQuery.children( ...
- linux系统批量无人值守安装
一:批量无人值守安安装原理 利用DHCP TFTP FTP和PXE技术实现批量安装系统,首先在主server上安装好DHCP TFTP和FTP服务,client通过网卡的PXE技术获取到IP地址和TF ...
- 为iPhone 6设计自适应布局
Apple从iOS 6加入了Auto Layout后开始就比较委婉的开始鼓励.建议开发者使用自适应布局,但是到目前为止,我感觉大多数开发者一直在回避这个问题,不管是不是由于历史原因造成的,至少他们在心 ...
- javascript将毫秒还原为可读时间格式
<script type="text/javascript"> //随便设置一个时间 var otime = new Date("2015-11-11 20: ...
- js调用百度地图搜索功能
引用百度jsApi <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&a ...
- lambda语法
(参数列表) => 表达式或者语句块 s => (s.IndexOf("a") > -1 其中:参数个数:可以有多个参数,一个参数,或者无参数.表达式或者语句块: ...
- 从字节理解Unicode(UTF8/UTF16)
如果你不知道或者不了解什么是Unicode/UTF8/UTF16,请详细阅读这篇文章(这也是这篇博文的先决条件): 学点编码知识又不会死:Unicode的流言终结者和编码大揭秘 但是如果你看完以上文章 ...
- C/C++中的sizeof
代码: #include <iostream> #include <string> using namespace std; int main(){ char s1[]=&qu ...
- 最小日志量的insert操作
--1.实验环境 SQL> conn scott/tiger Connected to Oracle Database 11g Enterprise Edition Release 11.2.0 ...