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求完强联通分量并染 ...
随机推荐
- Android开发之ProgressDialog与ProgressBar
ProgressDialog,继承AlertDialog.所以ProgressDialog就是一个在对话框中显示ProgressDialog,并显示进度的文本信息. 并且没有取消和确定按钮,只能通过b ...
- [Codeforces137B]Permutation(贪心?思路?,水题)
题目链接:http://codeforces.com/contest/137/problem/B 给n个数字,要求修改成1~n的全排列数中的一个,修改的次数尽可能少,问最少需要修改几个数. 记下数组里 ...
- decode-string(挺麻烦的)
Java String作为参数传参是不会改变的,这个与常识的感觉不同. public String decodeString(String s) { s = ""; return ...
- IIS 10.0 无法安装 URL rewrite重写模块 2.0解决办法
[问题描述]系统升级到Windows10后,IIS是10.0的,发现无法安装 URLRewrite重写模块 2.0. [解决办法]打开注册表编辑器,在HKEY_LOCAL_MACHINE\SOFTWA ...
- UVa 1347 (双线程DP) Tour
题意: 平面上有n个坐标均为正数的点,按照x坐标从小到大一次给出.求一条最短路线,从最左边的点出发到最右边的点,再回到最左边的点.除了第一个和最右一个点其他点恰好只经过一次. 分析: 可以等效为两个人 ...
- POJ 3084 Panic Room (最小割建模)
[题意]理解了半天--大意就是,有一些房间,初始时某些房间之间有一些门,并且这些门是打开的,也就是可以来回走动的,但是这些门是确切属于某个房间的,也就是说如果要锁门,则只有在那个房间里才能锁. 现在一 ...
- Python interview preparing
Collection & Recommended: 1. CN - 论坛中看到. - EN 英文原文真的真的很好好好T_T,看得让人感动T_T 总结个人感兴趣的问题(以下部分参照上面): 1. ...
- VirtualBox clonevdi文件和修改vdi的uuid
因为VirtualBox下,不允许有相同的uuid,所以要拷贝一份新的vdi像普通的拷贝是办不到的.需要用VirtualBox自带的一个.exe文件VBoxManage. 1.首先,进入终端或者是命令 ...
- dpkg-query
1.功能作用 查看软件包信息 2.位置 /usr/bin 3.格式用法 dpkg-query [<选项> ...] <命令> 4.主要参数 Commands: -s|--sta ...
- 快速搭建建SSH服务
一般来说如果用Ubuntu作为服务器,我们经常需要通过其他客户端远程连接它. 远程连接需要使用SSH,这里列出了一个快速完成这一任务的方法. 键入命令 # sudo apt-get install o ...