UOJ #332. 【NOIP2017】奶酪(暴力)

一、传送门

https://uoj.ac/problem/332
为了锻炼大家阅读代码的能力,本题将不撰写算法分析说明与代码编写指导。

二、代码

#include<cstdio>
#include<cmath>
#include<algorithm>
#include<bitset>
#pragma warning(disable:4996)
using namespace std;
template<size_t n> class union_find {
private:
	unsigned root[n]; int rank[n];
public:
	union_find<n>() { init(); }
	union_find<n>(const bool& WannaInit) { if (WannaInit == true)init(); }
	void init() {
		fill(rank, rank + n, 1); for (unsigned i = 0; i < n; ++i)root[i] = i;
	}
	void init(const size_t& _n) {
		fill(rank, rank + _n, 1); for (unsigned i = 0; i < _n; ++i)root[i] = i;
	}
	unsigned find_root(const unsigned& v) {
		unsigned r = v, t = v, u;
		if (t == root[v])return v;
		while (r != root[r]) { r = root[r]; }
		while (t != r) { u = root[t]; root[t] = r; t = u; }
		return r;
	}
	void path_compress() const { for (unsigned i = 0; i < n; ++i)find_root(i); }
	void merge(unsigned u, unsigned v) {
		unsigned fu = find_root(u), fv = find_root(v); int d = rank[fu] - rank[fv];
		if (d < 0) { swap(fu, fv); swap(u, v); }
		else if (d == 0)++rank[fu];
		root[fv] = fu;
	}
	void merge_no_path_compression(const unsigned& u, const unsigned& v) {
		root[v] = u; if (rank[u] == rank[v])++rank[u];
	}
	void merge_directly(const unsigned& u, const unsigned& v) { root[v] = u; }
	unsigned _rank(const unsigned& v) const { return rank[find_root(v)]; }
	size_t size() const { return n; }
};
struct point { long long x, y, z; };
inline long long dist2(const point& p, const point& q) {
	return (p.x - q.x) * (p.x - q.x) + (p.y - q.y) * (p.y - q.y) + (p.z - q.z) * (p.z - q.z);
}
const int nmax = 1001;
unsigned t, n; long long h, r, R, root; point p[nmax], q; union_find<nmax> u(false); bitset<nmax> top; bool ok;
int main() {
	scanf("%u", &t); ++t;
	while (--t) {
		scanf("%u%lld%lld", &n, &h, &r); R = 4 * r * r; top.reset(); u.init(n + 1); ok = false;
		for (unsigned i = 1; i <= n; ++i) { scanf("%lld%lld%lld", &p[i].x, &p[i].y, &p[i].z); }
		for (unsigned i = 1; i <= n; ++i)if (p[i].z <= r)u.merge(0, i);
		for (unsigned i = 1; i <= n; ++i) {
			q = p[i];
			for (unsigned j = i + 1; j <= n; ++j)if (dist2(q, p[j]) <= R)u.merge(i, j);
		}
		root = u.find_root(0);
		for (unsigned i = 1; i <= n; ++i)if (h - p[i].z <= r)top[i] = true;
		for (unsigned i = 1; i <= n; ++i)if (top[i] && u.find_root(i) == root) { ok = true; break; }
		if (ok)puts("Yes"); else puts("No");
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值