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.

    约翰的N(2≤N≤10000)只奶牛非常兴奋,因为这是舞会之夜!她们穿上礼服和新鞋子,别上鲜花,她们要表演圆舞.
    只有奶牛才能表演这种圆舞.圆舞需要一些绳索和一个圆形的水池.奶牛们围在池边站好,顺时针顺序由1到N编号.每只奶牛都面对水池,这样她就能看到其他的每一只奶牛.为了跳这种圆舞,她们找了M(2≤M≤50000)条绳索.若干只奶牛的蹄上握着绳索的一端,绳索沿顺时针方绕过水池,另一端则捆在另一些奶牛身上.这样,一些奶牛就可以牵引另一些奶牛.有的奶牛可能握有很多绳索,也有的奶牛可能一条绳索都没有对于一只奶牛,比如说贝茜,她的圆舞跳得是否成功,可以这样检验:沿着她牵引的绳索,找到她牵引的奶牛,再沿着这只奶牛牵引的绳索,又找到一只被牵引的奶牛,如此下去,若最终能回到贝茜,则她的圆舞跳得成功,因为这一个环上的奶牛可以逆时针牵引而跳起旋转的圜舞.如果这样的检验无法完成,那她的圆舞是不成功的.
    如果两只成功跳圆舞的奶牛有绳索相连,那她们可以同属一个组合.
    给出每一条绳索的描述,请找出,成功跳了圆舞的奶牛有多少个组合?

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.

    第1行输入N和M,接下来M行每行两个整数A和B,表示A牵引着B.

Output

* Line 1: A single line with a single integer that is the number of groups successfully dancing the Round Dance.

    成功跳圆舞的奶牛组合数.

Sample Input

5 4
2 4
3 5
1 2
4 1

Sample Output

1

又是一道阅读理解

答案就是点数大于等于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)相关推荐

  1. 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 & ...

  2. bzoj 1655: [Usaco2006 Jan] Dollar Dayz 奶牛商店(高精度完全背包)

    1655: [Usaco2006 Jan] Dollar Dayz 奶牛商店 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 599  Solved: 3 ...

  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 ...

  4. bzoj 1656: [Usaco2006 Jan] The Grove 树木(BFS)

    1656: [Usaco2006 Jan] The Grove 树木 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 246  Solved: 158 [ ...

  5. bzoj 1635: [Usaco2007 Jan]Tallest Cow 最高的牛

    1635: [Usaco2007 Jan]Tallest Cow 最高的牛 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 647  Solved: 39 ...

  6. bzoj:1666: [Usaco2006 Oct]Another Cow Number Game 奶牛的数字游戏

    Description 奶牛们又在玩一种无聊的数字游戏.输得很郁闷的贝茜想请你写个程序来帮她在开局时预测结果.在游戏的开始,每头牛都会得到一个数N(1<=N<=1,000,000).此时奶 ...

  7. BZOJ1720: [Usaco2006 Jan]Corral the Cows 奶牛围栏

    此题非常一眼 就是直接分别将 x,y 坐标离散化 并在离散化之后做二维前缀和 二分一下答案,O(n^2) 的 check 即可 注意在 check 当中的二分 x - mid 的过程中,需要去二分 x ...

  8. 【bzoj1612】【Usaco2008 Jan】Cow Contest奶牛的比赛 题解代码

    题目链接: http://www.lydsy.com/JudgeOnline/problem.php?id=1612 题解: 对于这种题我们发现可以dfs求出一共有多少头牛比他强,一共有多少头牛比他弱 ...

  9. bzoj 1718: [Usaco2006 Jan] Redundant Paths 分离的路径

    题意 给你一个无向图 问你最少添加多少条边可以使得他变成边双图 题解 直接双连通缩点 得到一颗树 然后答案是叶子节点/2向上取整 取法是每一次找两个LCA深度最小的叶子,两个连边就可以了 然后不知道为 ...

最新文章

  1. java jdk 环境变量配置(window 10 系统)
  2. JBoss日志文件配置
  3. python爬虫爬取csdn博客专家所有博客内容
  4. [轉]JavaScript获取HTML DOM父,子,临近节点
  5. 小程序服务器七牛云,基于七牛云 API 开发的微信小程序 SDK
  6. Oracle - 新装数据库、新建用户注意事项
  7. 2014北科计算机原理试题答案,北科_计算机组成原理考题-A卷答案
  8. 面试准备每日五题:C++(四)——typedefdefine、指针常量、队列栈、地址赋值、C和C++结构体
  9. Android自定义ScrollBar,android自定义View之垂直的滚动条
  10. 微信小程序验证码倒计时60秒
  11. 荣耀v20屏幕测试软件,测量原来可以这么简单 荣耀V20 AR测量功能体验
  12. 一个投标经理的标书检查笔记,拿来就用!
  13. 【Java分享客栈】我为什么极力推荐XXL-JOB作为中小厂的分布式任务调度平台
  14. 天王表的网络营销战略
  15. 认识 Iconfont 以及什么是 .eot、.woff、.ttf、.svg
  16. 优雅的落地个人所得税的计算
  17. zabbix告警配置
  18. Python-OpenCV API
  19. 面试java项目中解决了什么问题,附源代码
  20. 解决IE浏览器下载文件,文件名乱码问题(浏览器历史介绍)

热门文章

  1. python怎么读写文件-Python 文件I/O
  2. 如何自学python-如何自学python语言
  3. 前端接收到的Url参数有中文乱码
  4. linux开机自启动python脚本_linux怎么让一个python脚本开机自动开启
  5. 变量、属性、函数、方法总结
  6. vue中使用vuex结合sessionStorage做的登录功能
  7. 蓝桥杯2014c++真题:切面条
  8. [投稿]Speex回声消除代码分析
  9. linux声明语言变量,C语言中用extern声明外部变量
  10. python获取按键值_如何用按键获取刻度值?