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 ...
随机推荐
- mysql语句添加索引
1.PRIMARY KEY(主键索引) mysql>ALTER TABLE `table_name` ADD PRIMARY KEY ( `column` ) 2. ...
- asp.net页面之间的跳转
调用Request.CurrentExecutionFilePath方法返回到当前页面 站点中常常要跳转页面,调用Request.CurrentExecutionFilePath方法能够获取当前页面的 ...
- sql获取每门课程成绩最好的学生信息
1.相关数据表 Score表 [User]表 SQL语句例如以下: --查询出各科成绩最好的学生信息 --自连接 --SELECT TOP 1 * FROM Score B WHERE B.Score ...
- Javascript进阶篇——(DOM—节点---插入、删除和替换元素、创建元素、创建文本节点)—笔记整理
插入节点appendChild()在指定节点的最后一个子节点列表之后添加一个新的子节点.语法: appendChild(newnode) //参数: //newnode:指定追加的节点. 为ul添加一 ...
- asp.net 如何改变GridView搜索出来关键字的颜色?就像百度似的,我获得的是一个集合,改变集合中关键字的集合
输入“产量”查询,将“产量”拆分,产.量 string strColNameCnDt = string.Empty; for (int j = 0; j < dtTemp. ...
- C#this的五种用法
this的五种用法: 1.使用被掩盖的成员变量: class AA { int a; public void set1(int a) { this.a = a;//right } public voi ...
- poj3122 binary search 实数区间
Pie Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14536 Accepted: 4979 Special Ju ...
- 费马小定理&欧拉定理
在p是素数的情况下,对任意整数x都有xp≡x(mod p).这个定理被称作费马小定理其中如果x无法被p整除,我们有xp-1≡1(mod p).利用这条性质,在p是素数的情况下,就很容易求出一个数的逆元 ...
- 【android】android中activity的生命周期
activity生命周期: 实例代码: public class DemoActivity extends Activity { //1.activity第一次被创建的时候,执行 @Override ...
- 保存BASE64编码图片
1.前端上传用户图片时,一些K数较小图片,头像图标等 .以bass64编码后的字符串传到服务器. 2.服务器接收并保留到本地. // 页面上点击保存 $.post('/imgupload/save', ...