• 扩散


    题目描述

    思路

    二分的是结果,使用并查集保存各个点之间是否连通
    a为开始的点,b, c为目标点, 在经过了某个时刻时,a直接到不了b,a直接能到c,光通过这两个条件不能确定a间接到不了b, a可能通过c再到达b
    并查集维护的就是目标点之间的连通性,不光要验证a能不能到达b,还要验证目标点内部能不能互相到达。

    代码

    #include <cstdio>
    #include <cmath>
    
    int n, inf = 0x7f7f7f7f;
    struct Node {
    	int a, b;
    } m[55];
    int fa[55], l, r, mid, ans, cnt, len;
    int rec[55][55];
    int dis(Node a, Node b) {
    	return std::abs(a.a - b.a) + std::abs(a.b - b.b);
    }
    
    int get(int x) {
    	if (x == fa[x]) return x;
    	return get(fa[x]);
    }
    inline int read() {
    	int s = 0, f = 1;
    	char ch = getchar();
    	while(ch < '0' || ch > '9') {
    		if (ch == '-') f = -1;
    		ch = getchar();
    	}
    	while (ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
    	return s * f;
    }
    int main() {
    	n = read();
    	for (int i = 1; i <= n; ++i) m[i].a = read(), m[i].b = read();
    	for (int i = 1; i <= n; ++i) {
    		for (int j = i + 1; j <= n; ++j) {
    			rec[i][j] = dis(m[i], m[j]);
    		}
    	}
    	l = 0, r = inf;
    	while (l <= r) {
    		mid = l + r >> 1, len = mid << 1;
    		for (int i = 1; i <= n; ++i) fa[i] = i;
    		for (int i = 1; i <= n; ++i) {
    			for (int j = i + 1; j <= n; ++j) {
    				int fax = get(i), fay = get(j);
    				if (fax != fay) {
    					if(rec[i][j] <= len) fa[fax] = fay;
    				} 
    			}
    		}
    		bool flag = true;
    		for (int i = 2, j = get(1); i <= n && flag; ++i) {
    			if (get(i) != j) flag = false;
    		}
    		if (flag) ans = mid, r = mid - 1;
    		else l = mid + 1;
    	}
    	printf("%d", ans);
    	return 0;
    }
    
  • 相关阅读:
    codevs 3657 括号序列
    洛谷P1962 斐波那契数列
    Black Rock shooter
    codevs 2596 售货员的难题
    51Nod-1154 回文串划分
    UVA
    POJ3321[苹果树] 树状数组/线段树 + dfs序
    Hdu 4578 Transformation (线段树 分类分析)
    786B
    438D
  • 原文地址:https://www.cnblogs.com/liuzz-20180701/p/11582353.html
Copyright © 2020-2023  润新知