Description

Farmer John's cousin, Farmer Ron, who lives in the mountains of Colorado, has recently taught his cows to ski. Unfortunately, his cows are somewhat timid and are afraid to ski among crowds of people at the local resorts, so FR has decided to construct his own private ski area behind his farm.

FR's ski area is a rectangle of width W and length L of 'land squares' (1 <= W <= 500; 1 <= L <= 500). Each land square is an integral height H above sea level (0 <= H <= 9,999). Cows can ski horizontally and vertically between any two adjacent land squares, but never diagonally. Cows can ski from a higher square to a lower square but not the other way and they can ski either direction between two adjacent squares of the same height.

FR wants to build his ski area so that his cows can travel between any two squares by a combination of skiing (as described above) and ski lifts. A ski lift can be built between any two squares of the ski area, regardless of height. Ski lifts are bidirectional. Ski lifts can cross over each other since they can be built at varying heights above the ground, and multiple ski lifts can begin or end at the same square. Since ski lifts are expensive to build, FR wants to minimize the number of ski lifts he has to build to allow his cows to travel between all squares of his ski area.

Find the minimum number of ski lifts required to ensure the cows can travel from any square to any other square via a combination of skiing and lifts.

Input

* Line 1: Two space-separated integers: W and L

* Lines 2..L+1: L lines, each with W space-separated integers corresponding to the height of each square of land.

Output

* Line 1: A single integer equal to the minimal number of ski lifts FR needs to build to ensure that his cows can travel from any square to any other square via a combination of skiing and ski lifts

Sample Input

9 3
1 1 1 2 2 2 1 1 1
1 2 1 2 3 2 1 2 1
1 1 1 2 2 2 1 1 1

Sample Output

3

Hint

This problem has huge input data,use scanf() instead of cin to read data to avoid time limit exceed.

OUTPUT DETAILS:

FR builds the three lifts. Using (1, 1) as the lower-left corner, 
the lifts are (3, 1) <-> (8, 2), (7, 3) <-> (5, 2), and (1, 3) <-> 
(2, 2). All locations are now connected. For example, a cow wishing 
to travel from (9, 1) to (2, 2) would ski (9, 1) -> (8, 1) -> (7, 
1) -> (7, 2) -> (7, 3), take the lift from (7, 3) -> (5, 2), ski 
(5, 2) -> (4, 2) -> (3, 2) -> (3, 3) -> (2, 3) -> (1, 3), and then 
take the lift from (1, 3) - > (2, 2). There is no solution using

fewer than three lifts.

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stack>
  4. #define INF 1000010
  5. #define P 261010
  6. using namespace std;
  7. stack<int>S;
  8. int a[550][550],map[P][10],low[P],dfn[P];
  9. int instack[P];
  10. int bnt,be[P],n,m;
  11. int index,in[P],out[P];
  12. void build(){
  13. for(int i=1;i<=n;i++)
  14. for(int j=1;j<=m;j++){
  15. if(a[i][j]>=a[i-1][j])map[j+m*i-m][++map[j+m*i-m][0]] = j+(i-2)*m;
  16. if(a[i][j]>=a[i+1][j])map[j+m*i-m][++map[j+m*i-m][0]] = j+i*m;
  17. if(a[i][j]>=a[i][j+1])map[j+m*i-m][++map[j+m*i-m][0]] = j+(i-1)*m+1;
  18. if(a[i][j]>=a[i][j-1])map[j+m*i-m][++map[j+m*i-m][0]] = j+(i-1)*m-1;
  19. }
  20. }
  21. void tarjan(int i){
  22. dfn[i] = low[i] = ++index;
  23. S.push(i);
  24. instack[i] = 1;
  25. for(int j = 1;j<=map[i][0];j++){
  26. int k = map[i][j];
  27. if(!dfn[k]){
  28. tarjan(k);
  29. low[i] = min(low[i],low[k]);
  30. }
  31. else if(instack[k]){
  32. low[i] = min(low[i],dfn[k]);
  33. }
  34. }
  35. if(dfn[i]==low[i]){
  36. bnt++;
  37. int kk;
  38. do{
  39. kk = S.top();
  40. S.pop();
  41. instack[kk] = 0;
  42. be[kk] = bnt;
  43. }while(i!=kk);
  44. }
  45. }
  46. int main(){
  47. while(~scanf("%d%d",&m,&n)){
  48. int ans,ans1;
  49. memset(dfn,0,sizeof(dfn));
  50. memset(in,0,sizeof(in));
  51. memset(out,0,sizeof(out));
  52. memset(instack,0,sizeof(instack));
  53. bnt = ans = ans1 = index = 0;
  54. for(int i=0;i<=n+1;i++)a[i][0] = a[i][m+1] = INF;
  55. for(int j=0;j<=m+1;j++)a[0][j] = a[n+1][j] = INF;
  56. for(int i=1;i<=n;i++){
  57. for(int j=1;j<=m;j++){
  58. scanf("%d",&a[i][j]);
  59. map[j+(i-1)*m][0] = 0;
  60. }
  61. }
  62. build();
  63. for(int i=1;i<=n*m;i++)
  64. if(!dfn[i])
  65. tarjan(i);
  66. for(int i=1;i<=n*m;i++){
  67. for(int j =1;j<=map[i][0];j++)
  68. if(be[i]!=be[map[i][j]]){
  69. out[be[i]]++;
  70. in[be[map[i][j]]]++;
  71. }
  72. }
  73. for(int i=1;i<=bnt;i++){
  74. if(out[i]==0)ans++;
  75. if(in[i]==0)ans1++;
  76. }
  77. if(bnt==1)printf("0\n");
  78. else printf("%d\n",max(ans,ans1));
  79. }
  80. }
  1. 在POJ能过,在杭电就过不了,是什么原因啊,。。。。。。??!坑爹啊

D - Cow Ski Area的更多相关文章

  1. POJ2375 Cow Ski Area (强连通)(缩点)

                                        Cow Ski Area Time Limit: 1000MS   Memory Limit: 65536K Total Sub ...

  2. POJ 2375 Cow Ski Area(强连通)

    POJ 2375 Cow Ski Area id=2375" target="_blank" style="">题目链接 题意:给定一个滑雪场, ...

  3. POJ 2375 Cow Ski Area

    Cow Ski Area Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original I ...

  4. [USACO2004][poj2375]Cow Ski Area(在特殊图上用floodfill代替强联通算法)

    http://poj.org/problem?id=2375 题意:一个500*500的矩形,每个格子都有一个高度,不能从高度低的格子滑到高度高的格子(但相等高度可以滑),已知可以在2个相邻格子上加桥 ...

  5. POJ 2375 Cow Ski Area[连通分量]

    题目链接:http://poj.org/problem?id=2375题目大意:一片滑雪场,奶牛只能向相邻的并且不高于他当前高度的地方走.想加上缆车是的奶牛能从低的地方走向高的地方,求最少加的缆车数, ...

  6. poj 2375 Cow Ski Area bfs

    这个题目用tarjan找联通块,缩点,然后统计出入度为0的点理论上是可行的,但问题是会暴栈.考虑到这个题目的特殊性,可以直接用一次bfs找到数字相同且联通的块,这就是一个联通块,然后缩点,统计出入度即 ...

  7. POJ 2375 Cow Ski Area (强连通分量)

    题目地址:POJ 2375 对每一个点向与之相邻并h小于该点的点加有向边. 然后强连通缩点.问题就转化成了最少加几条边使得图为强连通图,取入度为0和出度为0的点数的较大者就可以.注意,当强连通分量仅仅 ...

  8. POJ 2375 Cow Ski Area【tarjan】

    题目大意:一个W*L的山,每个山有个高度,当且仅当一个山不比它相邻(有公共边的格子)的山矮时能够滑过去,现在可以装化学电梯来无视山的高度滑雪,问最少装多少电梯使得任意两点都可到达 思路:最后一句话已经 ...

  9. 杭电ACM分类

    杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...

随机推荐

  1. JDK源码学习系列05----LinkedList

                                             JDK源码学习系列05----LinkedList 1.LinkedList简介 LinkedList是基于双向链表实 ...

  2. poj2752 Seek the Name, Seek the Fame(next数组的运用)

    题目链接:id=2752" style="color:rgb(202,0,0); text-decoration:none; font-family:Arial; font-siz ...

  3. 【原创】shadowebdict开发日记:基于linux的简明英汉字典(一)

    全系列目录: [原创]shadowebdict开发日记:基于linux的简明英汉字典(一) [原创]shadowebdict开发日记:基于linux的简明英汉字典(二) [原创]shadowebdic ...

  4. js控制图片缩放、水平和垂直方向居中对齐

    已測试兼容 IE6,IE7,IE8,火狐FF,谷歌chrome. 这里使用了jquery插件,假设你不使用jquery,略微改造一下也非常快. 网上查了些资料,用css控制兼容性不好,看去非常揪心.于 ...

  5. linux 手动安装 oracle(转)

    Linux下安装Oracle 11 此为参照CU论坛上的高人写的文章并结合自身环境增加了点细节性的东西 操作系统 CentOS 4.2 2.6.11.8内核 安装准备 下载Oracle11安装包 内存 ...

  6. 【设计模式】Template Method模式

    OO基金会 封装 多态 继承 OO原则 封装变化 多用组合,少用继承 针对接口编程.不针对实现编程 为交互对象的松耦合设计而努力 类应该对扩展开放,对改动关闭 依赖抽象,不要依赖详细类 仅仅和朋友交谈 ...

  7. WPF中两条路径渐变的探讨

    原文:WPF中两条路径渐变的探讨 我们在WPF中,偶尔也会涉及到两条路径作一些“路径渐变 ”.先看看比较简单的情形:如下图(关键点用红色圆点加以标识):(图1) 上面图1中的第1幅图可以说是最简单的路 ...

  8. POJ 1384 Piggy-Bank 背包DP

    所谓的全然背包,就是说物品没有限制数量的. 怎么起个这么intimidating(吓人)的名字? 事实上和一般01背包没多少差别,只是数量能够无穷大,那么就能够利用一个物品累加到总容量结尾就能够了. ...

  9. NYOJ 300 &amp;&amp; hdu 2276 Kiki &amp; Little Kiki 2 (矩阵高速功率)

    pid=300">Kiki & Little Kiki 2 时间限制:5000 ms  |  内存限制:65535 KB 难度:4 描写叙述 There are n light ...

  10. android性能测试内存泄漏

    1.什么是内存泄漏?     适用于该系统的内存使用内存泄漏,未回复(释放),该内存可以没有事业,也不能被其他人使用使用自己. 2.出有什么差别?    内存泄漏是分配出去的内存无法回收.    内存 ...