bzoj 1654: [Usaco2006 Jan]The Cow Prom 奶牛舞会(Trajan)
1654: [Usaco2006 Jan]The Cow Prom 奶牛舞会
Time Limit: 5 Sec Memory Limit: 64 MB
Submit: 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 奶牛舞会 -- Tarjan
1654: [Usaco2006 Jan]The Cow Prom 奶牛舞会 Time Limit: 5 Sec Memory Limit: 64 MB Description The N (2 & ...
- bzoj 1655: [Usaco2006 Jan] Dollar Dayz 奶牛商店(高精度完全背包)
1655: [Usaco2006 Jan] Dollar Dayz 奶牛商店 Time Limit: 5 Sec Memory Limit: 64 MB Submit: 599 Solved: 3 ...
- bzoj 3377: [Usaco2004 Open]The Cow Lineup 奶牛序列
3377: [Usaco2004 Open]The Cow Lineup 奶牛序列 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 49 Solve ...
- bzoj 1656: [Usaco2006 Jan] The Grove 树木(BFS)
1656: [Usaco2006 Jan] The Grove 树木 Time Limit: 5 Sec Memory Limit: 64 MB Submit: 246 Solved: 158 [ ...
- bzoj 1635: [Usaco2007 Jan]Tallest Cow 最高的牛
1635: [Usaco2007 Jan]Tallest Cow 最高的牛 Time Limit: 5 Sec Memory Limit: 64 MB Submit: 647 Solved: 39 ...
- bzoj:1666: [Usaco2006 Oct]Another Cow Number Game 奶牛的数字游戏
Description 奶牛们又在玩一种无聊的数字游戏.输得很郁闷的贝茜想请你写个程序来帮她在开局时预测结果.在游戏的开始,每头牛都会得到一个数N(1<=N<=1,000,000).此时奶 ...
- BZOJ1720: [Usaco2006 Jan]Corral the Cows 奶牛围栏
此题非常一眼 就是直接分别将 x,y 坐标离散化 并在离散化之后做二维前缀和 二分一下答案,O(n^2) 的 check 即可 注意在 check 当中的二分 x - mid 的过程中,需要去二分 x ...
- 【bzoj1612】【Usaco2008 Jan】Cow Contest奶牛的比赛 题解代码
题目链接: http://www.lydsy.com/JudgeOnline/problem.php?id=1612 题解: 对于这种题我们发现可以dfs求出一共有多少头牛比他强,一共有多少头牛比他弱 ...
- bzoj 1718: [Usaco2006 Jan] Redundant Paths 分离的路径
题意 给你一个无向图 问你最少添加多少条边可以使得他变成边双图 题解 直接双连通缩点 得到一颗树 然后答案是叶子节点/2向上取整 取法是每一次找两个LCA深度最小的叶子,两个连边就可以了 然后不知道为 ...
最新文章
- java jdk 环境变量配置(window 10 系统)
- JBoss日志文件配置
- python爬虫爬取csdn博客专家所有博客内容
- [轉]JavaScript获取HTML DOM父,子,临近节点
- 小程序服务器七牛云,基于七牛云 API 开发的微信小程序 SDK
- Oracle - 新装数据库、新建用户注意事项
- 2014北科计算机原理试题答案,北科_计算机组成原理考题-A卷答案
- 面试准备每日五题:C++(四)——typedefdefine、指针常量、队列栈、地址赋值、C和C++结构体
- Android自定义ScrollBar,android自定义View之垂直的滚动条
- 微信小程序验证码倒计时60秒
- 荣耀v20屏幕测试软件,测量原来可以这么简单 荣耀V20 AR测量功能体验
- 一个投标经理的标书检查笔记,拿来就用!
- 【Java分享客栈】我为什么极力推荐XXL-JOB作为中小厂的分布式任务调度平台
- 天王表的网络营销战略
- 认识 Iconfont 以及什么是 .eot、.woff、.ttf、.svg
- 优雅的落地个人所得税的计算
- zabbix告警配置
- Python-OpenCV API
- 面试java项目中解决了什么问题,附源代码
- 解决IE浏览器下载文件,文件名乱码问题(浏览器历史介绍)