Wireless Network POJ - 2236【并查集】

Wireless Network POJ - 2236

题目

思路:并查集,把维修过且距离在d之内的放入同一个集合,之后判断

具体代码如下

#include<iostream>
#include<cstring>
#include<cstdio>

using namespace std;

const int N = 1010;

struct Point{ //存各个点的坐标 
	int x, y;
}point[N];

int n, d;
int p[N];
int visit[N], cnt;//visit存的是已经维修过的点 
bool st[N];//st数组存该点有没有维修过 

int find(int x){
	if(p[x] != x) p[x] = find(p[x]);
	return p[x]; 
}

bool cmp(int x, int y){ //比较距离,开根号之后可能会损失精度,这里直接比较平方 
	double x1 = point[x].x - point[y].x;
	double y1 = point[x].y - point[y].y;
	return (x1*x1 + y1*y1 <= d*d);
}

int main(){
	
	cin >> n >> d;
	for(int i=1; i<=n; ++i) p[i] = i;
	for(int i=1; i<=n; ++i){
		int a, b;
		cin >> a >> b;
		point[i] = {a, b};
	}
	
	char op[2];
	
	while(cin >> op){
		int x1, x2;
		if(*op == 'O'){
			cin >> x1;
			for(int i=0; i<cnt; ++i){
				if(cmp(visit[i], x1)){
					int px = find(visit[i]), py = find(x1);
					p[px] = py;
				}
			}
			visit[cnt++] = x1;
			st[x1] = true;
		}else{
			cin >> x1 >> x2;
			if(st[x1] && st[x2] && find(x1) == find(x2)) puts("SUCCESS");
			else puts("FAIL");
		}
	}
	
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值