• 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
    ");
    			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
    ", max(ins, outs));
    	}
    	return 0;
    }


  • 相关阅读:
    谈谈关于PHP连接数据库的两种方法(PDO&Mysqli)
    关于Mui严格模式下的报错解决方案
    CSS常用选择器的认识
    HTML汇总以及CSS的一些开端
    LAMP架构之PHP-FPM 服务器 转
    httpd配置
    notapai++ 使用小技巧
    转载-notepad++ zend-coding使用
    Apache下的配置文件httpd.conf、httpd-vhosts.conf 转
    Apache本机不同端口多站点配置:httpd-vhosts.conf(转载)
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/5224869.html
Copyright © 2020-2023  润新知