poj-2236

题意:在平面坐标系上有n个坏掉的电脑,电脑之前只有距离小于d才能通信(在两台电脑都修好的情况下),

有两个查询第一种O x即修理电脑x,第二种S x y即查询x电脑和y电脑之间是否可以通信(间接的也可以)。

题解:并查集裸题。

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cstring>
#include <math.h>
#include <stdlib.h>
#include <time.h>

#define ll long long
#define Max 100000

using namespace std;

const int inf = 0x3f3f3f3f;

int d;
int use[1110];

struct node{
	int x,y;
	int pre;
	int root;
}a[1110];

int init(int n){
	for(int i = 1 ; i <= n ; i++){
		a[i].pre = i;
		a[i].root = 1;
	}
}

int find_pre(int x){
	if(a[x].pre==x){
		return x;
	}
	return a[x].pre = find_pre(a[x].pre);
}

bool is_s(int x,int y){
	return find_pre(x)==find_pre(y);
}

void unite(node x,node y){
	int rootx,rooty;
	rootx = find_pre(x.pre);
	rooty = find_pre(y.pre);
	if(rootx == rooty){
		return;
	}
	if((x.x-y.x)*(x.x-y.x)+(x.y-y.y)*(x.y-y.y) <= d*d){
		if(a[rootx].root > a[rooty].root){
			a[rooty].pre = rootx;
		}else{
			if(a[rootx].root == a[rooty].root){
				a[rooty].root++;
			}
			a[rootx].pre = rooty;
		}
	}
}

int main(){
	int n;
	cin>>n>>d;
	for(int i = 1 ; i <= n ; i++){
		scanf("%d %d",&a[i].x,&a[i].y);
	}
	init(n);
	getchar();
	char x;
	int k,j;
	while(true){
		scanf("%c",&x);
		if(x=='O'){
			scanf("%d",&k);
			use[k] = 1;
			for(int i = 1 ; i <= n ; i++){
				if(use[i]&&i!=k){
					unite(a[i],a[k]);
				}
			}
		}else if(x=='S'){
			scanf("%d %d",&k,&j);
			if(is_s(k,j)){
				cout<<"SUCCESS"<<endl;
			}else{
				cout<<"FAIL"<<endl;
			}
		}
        getchar();
	}
} 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值