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 ...
随机推荐
- Hacker(16)----防范端口扫描与嗅探
端口扫描与嗅探都是黑客常用的招数,其目的是定位目标计算机和窃取隐私信息.为确保自己计算机的安全,用户需要掌握防范嗅探与端口扫描的常见措施,保障个人隐私信息安全. 一.掌握防范端口扫描的常用措施 防范端 ...
- 解决System.Data.SQLite兼容32位和64位问题
将当前说明文档的目录下的x64.x86目录和System.Data.SQLite.dll文件复制到您的应用程序根目录中(注意更新引用,引用System.Data.SQLite.dll即可,两目录中的不 ...
- Winform改变Textbox边框颜色(转)
namespace MyTextBoxOne { //使用时必须把文本框的BorderStyle为FixedSingle才能使用 //一些控件(如TextBox.Button等)是由系统进程绘制,重载 ...
- How to resolve the SQL error “cannot connect to WMI provider”
当你试图打开SQL Server Configuation Manager时发现如下错误: “cannot connect to WMI provider. You do not have permi ...
- unity 之2D游戏简单操作
unity 做2D项目也很方便. 首先要调整camera的模式,camera 的检视面板参数如下: perspective 模式就是平时用的 模式.摄像机到游戏物体是有角度的张开, 而 orthog ...
- C#基本数据类型与C++区别
与C++不同的地方: char占两个字节存Unicode字符, long long 改为 long ; unsize ... 改为 u... 新增: byte占1个字节,类似与C++char, sby ...
- nodejs 简单http 文件上传demo
// 这是一个简单的Node HTTP,能处理当前目录的文件 // 并能实现良种特殊的URL用于测试 // 用http://localhost:8000 或http://127.0.0.1:8000 ...
- shell编程-1到100的求和与冒泡排序
Shell编程 一. for循环 生成列表 {起始数..结束数} 命令生成列表 `seq [起始数] [步进长度] 结束数 ` for l in {1..5};do for l in `seq ...
- C/C++ 中##(两个井号)和#(一个井号)用法
##(两个井号)和#(一个井号)都是什么意思 连接符 ##(两个井号) 不知道什么符 #(一个井号) ## 连接符号由两个井号组成,其功能是在带参数的宏定义中将两个子串(token)联接起来,从而形成 ...
- requirejs的配置
baseUrl : 所有模块的查找根路径. 当加载纯.js文件(依赖字串以/开头,或者以.js结尾,或者含有协议),不会使用baseUrl. 如未显式设置baseUrl,则默认值是加载require. ...