bzoj 1654: [Usaco2006 Jan]The Cow Prom 奶牛舞会(Trajan)
来源:互联网 发布:javascript编程全解 编辑:程序博客网 时间:2024/06/06 20:43
1654: [Usaco2006 Jan]The Cow Prom 奶牛舞会
Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 380 Solved: 289
[Submit][Status][Discuss]
Description
The N (2 <= N <= 10,000) 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 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
* Line 1: Two space-separated integers: N and M
* 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
* Line 1: A single line with a single integer that is the number of groups successfully dancing the Round Dance.
Sample Input
Sample Output
又是一道阅读理解
答案就是点数大于等于2的强联通分量个数
#include<stdio.h>#include<vector>#include<stack>#include<algorithm>using namespace std;vector<int> G[20005];stack<int> st;int t, cnt, low[20005], time[20005], scc[20005], sum[20005];void Trajan(int u){int i, v;low[u] = time[u] = ++t;st.push(u);for(i=0;i<G[u].size();i++){v = G[u][i];if(low[v]==0){Trajan(v);low[u] = min(low[u], low[v]);}else if(scc[v]==0)low[u] = min(low[u], time[v]);}if(low[u]==time[u]){cnt++;while(st.empty()==0){v = st.top();st.pop();scc[v] = cnt;sum[cnt]++;if(v==u)break;}}}int main(void){int ans, n, m, i, x, y;scanf("%d%d", &n, &m);for(i=1;i<=m;i++){scanf("%d%d", &x, &y);G[x].push_back(y);}for(i=1;i<=n;i++){if(low[i]==0)Trajan(i);}ans = 0;for(i=1;i<=cnt;i++){if(sum[i]>1)ans++;}printf("%d\n", ans);return 0;}
- bzoj 1654: [Usaco2006 Jan]The Cow Prom 奶牛舞会(Trajan)
- BZOJ 1654: [Usaco2006 Jan]The Cow Prom 奶牛舞会
- 【BZOJ】1654: [Usaco2006 Jan]The Cow Prom 奶牛舞会
- BZOJ 1635: [Usaco2006 Jan]The Cow Prom 奶牛舞会 tarjan
- 1654: [Usaco2006 Jan]The Cow Prom 奶牛舞会
- |BZOJ 1654|强连通分量|[Usaco2006 Jan]The Cow Prom 奶牛舞会
- bzoj1654 [Usaco2006 Jan]The Cow Prom 奶牛舞会
- bzoj1654: [Usaco2006 Jan]The Cow Prom 奶牛舞会
- [BZOJ1654] [Usaco2006 Jan]The Cow Prom 奶牛舞会
- 【bzoj1654】[Usaco2006 Jan]The Cow Prom 奶牛舞会
- poj 3180 The Cow Prom(奶牛舞会)
- bzoj 1720: [Usaco2006 Jan]Corral the Cows 奶牛围栏 二分
- bzoj1654[The Cow Prom 奶牛舞会] tarjan求有向无环图的强联通分量
- [USACO06JAN]牛的舞会The Cow Prom
- 【POJ3180】The Cow Prom奶牛舞会 还是一道挺裸的有向图强连通分量
- bzoj 1656: [Usaco2006 Jan] The Grove 树木 (bfs+技巧)
- bzoj 1656: [Usaco2006 Jan] The Grove 树木(BFS)
- 【题解】[USACO06JAN]牛的舞会The Cow Prom(tarjan)
- Java 并发 —— 读写锁(ReadWriteLock)
- Android开发艺术探索_JNI和NDK编程(十四)
- 网络相关
- 趣味数学问题的算法实现
- 最新C#基础知识,面试题汇总
- bzoj 1654: [Usaco2006 Jan]The Cow Prom 奶牛舞会(Trajan)
- 2017 ACM/ICPC Asia Regional Urumqi Online
- python中setting.py详解
- mac端安装memcache踩坑
- JAVA集合中的HashSet
- 大整数的乘法-分治法
- 数组操作方法
- python的__init__.py
- SQL中CASE...WHEN语句