poj 3180 The Cow Prom(强联通分量)
http://poj.org/problem?id=3180
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 1428 | Accepted: 903 |
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 求有多少个大于2的联通分量
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<vector>
#define N 10010
#define min(a, b)(a < b ? a : b); using namespace std; vector<vector<int> >G; int low[N], dfn[N], Stack[N];
int n, m, num, Time, top;
bool Instack[N]; void Init()
{
G.clear();
G.resize(n + );
memset(low, , sizeof(low));
memset(dfn, , sizeof(dfn));
memset(Stack, , sizeof(Stack));
memset(Instack, false, sizeof(Instack));
Time = top = num = ;
} void Tarjan(int u)
{
int len, v, i, k = ;
low[u] = dfn[u] = ++Time;
Stack[top++] = u;
Instack[u] = true;
len = G[u].size();
for(i = ; i < len ; i++)
{
v = G[u][i];
if(!dfn[v])
{
Tarjan(v);
low[u] = min(low[u], low[v]);
}
else if(Instack[v])
low[u] = min(low[u], dfn[v]);
}
if(low[u] == dfn[u])
{
do
{
k++;
v = Stack[--top];
Instack[v] = false;
}while(v != u);
if(k >= )
num++;
}
} void Solve()
{
int i;
for(i = ; i <= n ; i++)
if(!low[i])
Tarjan(i);
} int main()
{
int a, b;
while(~scanf("%d%d", &n, &m))
{
Init();
while(m--)
{
scanf("%d%d", &a, &b);
G[a].push_back(b);
}
Solve();
printf("%d\n", num);
}
return ;
}
poj 3180 The Cow Prom(强联通分量)的更多相关文章
- POJ 3180 The Cow Prom(强联通)
题目大意: 约翰的N(2≤N≤10000)只奶牛非常兴奋,因为这是舞会之夜!她们穿上礼服和新鞋子,别上鲜花,她们要表演圆舞. 只有奶牛才能表演这种圆舞.圆舞需要一些绳索和一个圆形的 ...
- POJ 2186 Popular cows(Kosaraju+强联通分量模板)
题目链接:http://poj.org/problem?id=2186 题目大意:给定N头牛和M个有序对(A,B),(A,B)表示A牛认为B牛是红人,该关系具有传递性,如果牛A认为牛B是红人,牛B认为 ...
- POJ 1904 King's Quest 强联通分量+输入输出外挂
题意:国王有n个儿子,现在这n个儿子要在n个女孩里选择自己喜欢的,有的儿子可能喜欢多个,最后国王的向导给出他一个匹配.匹配有n个数,代表某个儿子和哪个女孩可以结婚.已知这些条件,要你找出每个儿子可以和 ...
- POJ 3180 The cow Prom Tarjan基础题
题目用google翻译实在看不懂 其实题目意思如下 给一个有向图,求点个数大于1的强联通分量个数 #include<cstdio> #include<algorithm> #i ...
- POJ 3592 Instantaneous Transference(强联通分量 Tarjan)
http://poj.org/problem?id=3592 题意 :给你一个n*m的矩阵,每个位置上都有一个字符,如果是数字代表这个地方有该数量的金矿,如果是*代表这个地方有传送带并且没有金矿,可以 ...
- poj 3180 The Cow Prom(tarjan+缩点 easy)
Description The N ( <= N <= ,) cows are so excited: it's prom night! They are dressed in their ...
- POJ 3180 The Cow Prom(SCC)
[题目链接] http://poj.org/problem?id=3180 [题目大意] N头牛,M条有向绳子,能组成几个歌舞团?要求顺时针逆时针都能带动舞团内所有牛. [题解] 等价于求点数大于1的 ...
- [poj] 3180 the cow prom
原题 这是一道强连通分量板子题. 我们只用输出点数大于1的强连通分量的个数! #include<cstdio> #include<algorithm> #include< ...
- USACO06JAN The Cow Prom /// tarjan求强联通分量 oj24219
题目大意: n个点 m条边的图 求大小大于1的强联通分量的个数 https://www.cnblogs.com/stxy-ferryman/p/7779347.html tarjan求完强联通分量并染 ...
随机推荐
- HDU 4483 Lattice triangle(欧拉函数)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4483 题意:给出一个(n+1)*(n+1)的格子.在这个格子中存在多少个三角形? 思路:反着想,所有情 ...
- [POJ3352]Road Construction(缩点,割边,桥,环)
题目链接:http://poj.org/problem?id=3352 给一个图,问加多少条边可以干掉所有的桥. 先找环,然后缩点.标记对应环的度,接着找桥.写几个例子就能知道要添加的边数是桥的个数/ ...
- C++ STL之迭代器注意事项
1.两个迭代器组成的区间是前闭后开的 2.如果迭代器的有效性,如果迭代器所指向的元素已经被删除,那么迭代器会失效 http://blog.csdn.net/hsujouchen/article/det ...
- mac 下php运行bug
如下所说bug在window下没有,在mac下存在. mac下的php报如下错误: fopen("data.json") Error: failed to open stream: ...
- bzoj3926
题目的意思是叶子不超过20个……听说当初zjoi不少人被坑 分别对每个叶子以它为根dfs出20个dfs树,这样整个树的任何一个子串,都是某个dfs树上一个点到它的一个子孙的路径 每个dfs树,根到叶子 ...
- Linux LiveCD 的制作
Knoppix,只需一张光盘, 就能够让我们在任何场所,随心所欲地使用 Linux1, 打破了操作系统只能先安装再使用的传统概念. Knoppix 最初的设计用途是教学,但由于这项技术很受欢迎,使得 ...
- 【C#学习笔记】载入图片并居中
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- T-SQL备忘(1):表联接
测试用例表如下: 1.取2个成员表中的交集(A∩B) T-SQL: select Member1.Name,Member1.Age from Member1 join Member2 on Membe ...
- 打印Ibatis最终的SQL语句
在项目开发时都大家都希望将SQL在后台打印出来,以帮助开发以及后续的bug修改.如果用JDBC那么可以方便的打印,可使用ibatis就不知道怎么办了,最近在网上找了一段log4j的配置可以很保姆的处理 ...
- 获取手机内存\可用内存\单个APP运行内存
/** 手机总内存 */ private String getTotalMemory() { // 系统内存信息文件 String str1 = "/proc/meminfo"; ...