poj - 2236 并查集+set优化

题目链接 传送门

题意:就是给你n个点以及它们的坐标,然后就是有两种操作,一种操作是将某一个点恢复,在它周围半径d以内的其他已经恢复的点都与它相连,即将能与它相连的其它连通块都连接起来,另一种操作是询问a,b两点是否属于同一个连通块;

思路:并查集+set优化

 

简单的并查集

#include <iostream>
#include <stdio.h>
#include <set>
#include <algorithm>
#include <cstring>
#include <cmath>

using namespace std;

struct Node
{
	int x, y, id;
	Node(int _x = 0,int _y = 0) : x(_x), y(_y), id(0){}
	bool operator < (const Node A) const
	{
		if(x != A.x) return x < A.x;
		return y < A.y;
	}
}node[1010];

set <Node> st;
set <Node> :: iterator it;
int vis[1010], pre[1010];
int n, d;

int Find(int x)
{
	return pre[x] == x ? x : pre[x] = Find(pre[x]);
}

int main()
{
	int a, b;
	char c;
	memset(vis,-1,sizeof(vis));
	scanf("%d%d", &n, &d);
	for(int i = 1; i <= n; ++i){
		scanf("%d%d", &node[i].x, &node[i].y);
		node[i].id = pre[i] = i;
	}
	getchar();
	while(~scanf("%c", &c)){
		if(c == 'O'){
			scanf("%d", &a);
			if(vis[a] == 1) continue;
			vis[a] = 1;
			for(it = st.lower_bound(Node(node[a].x - d, node[a].y - d)); it != st.end() && node[a].x + d >= it->x ; ++it){
				if(vis[it->id] && sqrt(pow(node[a].x - it->x, 2.0) + pow(node[a].y - it->y, 2.0)) <= d)
					pre[Find(it->id)] = Find(node[a].id);
			}
			st.insert(node[a]);
		}
		else{
			scanf("%d%d", &a, &b);
			if(vis[a] && vis[b] && Find(a) == Find(b))
				printf("SUCCESS\n");
			else
				printf("FAIL\n");
		}
		getchar();
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值