POJ 2375 Cow Ski Area(强连通)

POJ 2375 Cow Ski Area

题目链接

题意:给定一个滑雪场,每个点能向周围4个点高度小于等于这个点的点滑,现在要建电缆,使得任意两点都有路径互相可达,问最少需要几条电缆

思路:强连通缩点,每个点就是一个点,能走的建边,缩点后找入度出度为0的个数的最大值就是答案,注意一开始就强连通了答案应该是0

代码:

#include <cstdio>
#include <cstring>
#include <stack>
#include <algorithm>
using namespace std;

const int N = 250005;
const int M = 1000005;
const int d[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};

int n, m;

struct Edge {
	int u, v;
	Edge() {}
	Edge(int u, int v) {
		this->u = u; this->v = v;
	}
} edge[M];

int en, first[N], next[M];

void add(int u, int v) {
	edge[en] = Edge(u, v);
	next[en] = first[u];
	first[u] = en++;
}

int h[505][505];
int pre[N], dfn[N], sccn, sccno[N], dfs_clock;
stack<int> S;

void dfs_scc(int u) {
	pre[u] = dfn[u] = ++dfs_clock;
	S.push(u);
	for (int i = first[u]; i + 1; i = next[i]) {
		int v = edge[i].v;
		if (!pre[v]) {
			dfs_scc(v);
			dfn[u] = min(dfn[u], dfn[v]);
		} else if (!sccno[v]) dfn[u] = min(dfn[u], pre[v]);
	}
	if (dfn[u] == pre[u]) {
		sccn++;
		while (1) {
			int x = S.top(); S.pop();
			sccno[x] = sccn;
			if (x == u) break;
		}
	}
}

void find_scc() {
	sccn = dfs_clock = 0;
	memset(pre, 0, sizeof(pre));
	memset(sccno, 0, sizeof(sccno));
	for (int i = 0; i < n * m; i++)
		if (!pre[i]) dfs_scc(i);
}

int in[N], out[N];

int main() {
	while (~scanf("%d%d", &m, &n)) {
		en = 0;
		memset(first, -1, sizeof(first));
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++)
				scanf("%d", &h[i][j]);
		}
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				for (int k = 0; k < 4; k++) {
					int x = i + d[k][0];
					int y = j + d[k][1];
					if (x < 0 || x >= n || y < 0 || y >= m) continue;
					if (h[i][j] >= h[x][y]) add(i * m + j, x * m + y);
				}
			}
		}
		find_scc();
		if (sccn == 1) {
			printf("0\n");
			continue;
		}
		memset(in, 0, sizeof(in));
		memset(out, 0, sizeof(out));
		for (int u = 0; u < n * m; u++) {
			for (int j = first[u]; j + 1; j = next[j]) {
				int v = edge[j].v;
				if (sccno[u] != sccno[v]) {
					in[sccno[v]]++;
					out[sccno[u]]++;
				}
			}
		}
		int ins = 0, outs = 0;
		for (int i = 1; i <= sccn; i++) {
			if (!in[i]) ins++;
			if (!out[i]) outs++;
		}
		printf("%d\n", max(ins, outs));
	}
	return 0;
}
时间: 2024-11-04 18:39:13

POJ 2375 Cow Ski Area(强连通)的相关文章

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

题目地址:POJ 2375 对每一个点向与之相邻并h小于该点的点加有向边. 然后强连通缩点.问题就转化成了最少加几条边使得图为强连通图,取入度为0和出度为0的点数的较大者就可以.注意,当强连通分量仅仅有一个的时候.答案是0,而不是1. 代码例如以下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #in

POJ 2375 Cow Ski Area【tarjan】

题目大意:一个W*L的山,每个山有个高度,当且仅当一个山不比它相邻(有公共边的格子)的山矮时能够滑过去,现在可以装化学电梯来无视山的高度滑雪,问最少装多少电梯使得任意两点都可到达 思路:最后一句话已经把强连通模型裸裸地说出来了 那问题变成了一个图最小加几条边变成强连通图的经典问题,比较一下出度为0和入度为0的点的个数的大小即可,还有个特例只有一个SCC的情况 #include<cstdio> #include<string.h> #include<iostream> #

POJ2375 Cow Ski Area 【强连通分量】+【DFS】

Cow Ski Area Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2323   Accepted: 660 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 some

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

Cow Ski Area Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3323   Accepted: 919 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 some

D - Cow Ski Area

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

POJ2375 Cow Ski Area【Tarjan】【强连通分量】

题目链接: http://poj.org/problem?id=2375 题目大意: 有一片供奶牛滑雪的滑雪场,可供滑雪的区域是W(宽)*L(长)的矩阵.上边有W*L个点.规定 奶牛从一个点只能向它上.下.左.右相邻的并且高度不大于它的点运动.现在想要在某些 点对之间架设缆车,使得奶牛可以从较低的地方想较高的地方运动,那么问题来了:最少需 要多少辆这样的缆车就可以使奶牛从每个点运动到可供滑雪区域的每个角落. 思路: 把奶牛符合从点u移动到点v的条件当做一条单向边.那么所有点和边就可以构成有向图.

POJ2375 Cow Ski Area (添最少边构造强连通分量的某个与入度出度相关的结论)

题意:本题描述了一片滑雪场,并且规定奶牛从一个点只能向它相邻的并且高度不大于它的点运动,现在想要在某些点对之间加上缆车使得奶牛也可以从较低点到达较高点,问最少需要多少辆这样的缆车就可以使得奶牛可以从任意一个点运动到滑雪场的每个角落. 思路:即问至少加多少条边使图变成强联通图,先缩点成DAG. 不难知道强连通分量的所有节点的入度和出度均不为0,可以统计DAG上的入度和出度为0的个数分别是a,b.然后答案即为max(a,b). 只是发现入度为0和出度为0的点可以按某种方式相连接总能构造出一个强连通分

poj 1985 Cow Marathon 【树的直径】

题目:poj 1985 Cow Marathon 题意:给出一个树,让你求树的直径. 分析: 树的直径:树上两点之间的最大距离. 我们从任意一点出发,BFS一个最远距离,然后从这个点出发,在BFS一个最远距离,就是树的直径. AC代码: /* POJ:1985 Cow Marathon 2014/10/12/21:18 Yougth*/ #include <cstdio> #include <iostream> #include <algorithm> #include

POJ 3613 Cow Relays 恰好n步的最短路径

http://poj.org/problem?id=3613 题目大意: 有T条路,从s到e走n步,求最短路径. 思路: 看了别人的... 先看一下Floyd的核心思想: edge[i][j]=min(edge[i][j],edge[i][k]+edge[k][j]) i到j的最短路是i到j的直接路径或者经过k点的间接路径,但是矩阵的更新总是受到上一次更新的影响 如果每次的更新都存进新矩阵,那么edge[i][k]+edge[k][j]是不是表示只经过三个点两条边的路径呢? min(edge[i