Cow Ski Area
This problem will be judged on PKU. Original ID: 2375
64-bit integer IO format: %lld Java class name: Main
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
* Lines 2..L+1: L lines, each with W space-separated integers corresponding to the height of each square of land.
Output
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
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.
Source
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
struct arc {
int to,next;
arc(int x = ,int y = -) {
to = x;
next = y;
}
};
arc e[];
int head[maxn],dfn[maxn],belong[maxn],low[maxn],in[maxn],out[maxn];
int tot,scc,idx,n,W,L;
bool instack[maxn];
int mystack[maxn],top;
void add(int u,int v) {
e[tot] = arc(v,head[u]);
head[u] = tot++;
}
void tarjan(int u) {
dfn[u] = low[u] = ++idx;
mystack[top++] = u;
instack[u] = true;
for(int i = head[u]; ~i; i = e[i].next) {
if(!dfn[e[i].to]) {
tarjan(e[i].to);
low[u] = min(low[u],low[e[i].to]);
} else if(instack[e[i].to]) low[u] = min(low[u],dfn[e[i].to]);
}
if(dfn[u] == low[u]) {
scc++;
int v;
do {
v = mystack[--top];
instack[v] = false;
belong[v] = scc;
} while(v != u);
}
}
void init() {
for(int i = ; i < maxn; ++i) {
dfn[i] = low[i] = belong[i] = ;
instack[i] = false;
in[i] = out[i] = ;
}
top = tot = idx = scc = ;
memset(head,-,sizeof(head));
}
int mp[][];
int main() {
const int dir[][] = {-,,,,,,,-};
while(~scanf("%d %d",&W,&L)) {
n = W*L;
init();
for(int i = ; i < L; ++i)
for(int j = ; j < W; ++j)
scanf("%d",mp[i]+j); for(int i = ; i < L; ++i)
for(int j = ; j < W; ++j)
for(int k = ; k < ; ++k) {
int ti = i + dir[k][];
int tj = j + dir[k][];
if(ti < || ti >= L || tj < || tj >= W) continue;
if(mp[ti][tj] <= mp[i][j]) add(i*W+j,ti*W+tj);
}
for(int i = ; i < n; ++i) if(!dfn[i]) tarjan(i);
if(scc < ) puts("");
else{
int x = ,y = ;
for(int i = ; i < n; ++i){
for(int j = head[i]; ~j; j = e[j].next){
if(belong[i] == belong[e[j].to]) continue;
in[belong[e[j].to]]++;
out[belong[i]]++;
}
}
for(int i = ; i <= scc; ++i){
if(!in[i]) x++;
if(!out[i]) y++;
}
printf("%d\n",max(x,y));
}
}
return ;
}