简单并查集

链接: 题目.
题意:有1到n台坏电脑,每台电脑可连接半径是d,有两个操作:

1、O p,表示修复第p台电脑。
2、S p q,判断p和q两台电脑能不能连接(直接或间接连接)。能连输出SUCCESS,否则输出FAIL

思路:直接连接很好判断,间接连接就判断在不在同一个集合里。
代码:
#include <iostream>
#include <cstdio>
#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <stack>
#include <unordered_set>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define pb push_back
#define T int T;scanf("%d",&T);while(T--)
const ll mod=1e9+7;
pair<int,int> place[1005];
int f[1005];
int n,d;
int vis[1005];
int find(int x){
	if(f[x]==x) return x;
	return f[x] = find(f[x]);
}
int main(){
	scanf("%d %d", &n, &d);
	for(int i = 1; i <= n; i ++){
		f[i] = i;
		scanf("%d %d", &place[i].first, &place[i].second);
	}
	char c;
	while(cin >> c){
		if(c=='O'){
			int p;
			scanf("%d", &p);
			if(vis[p]==0){	//没修过才更新并查集。
				for(int i = 1; i <= n; i ++){	//间接连接
					if(vis[i]&&pow(place[i].first-place[p].first,2)+pow(place[i].second-place[p].second,2)<=d*d){
						int fx = find(i);
						int fy = find(p);
						f[fx] = fy;
					}
				}
			}
			vis[p] = 1;	//修过
		}else{
			int p,q;
			scanf("%d %d", &p, &q);
			if(vis[q]==0||vis[p]==0){
				printf("FAIL\n");
				continue;
			}
			int fp = find(p);
			int fq = find(q);
			if(fp==fq || pow(place[q].first-place[p].first,2)+pow(place[q].second-place[p].second,2)<=d*d) printf("SUCCESS\n");	//直接或间接连接
			else printf("FAIL\n");
		}
	}

    return 0;
}
/*


*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值