POJ 2236 Wireless Network 简单并查集

挺简单的一道并查集,有N台电脑,距离在D内的两台正常电脑可以互相连接,给出N台电脑的坐标,刚开始电脑都是坏的,一直操作直到末尾。

1:修复一台电脑,自动连接可连接电脑;

2:查询两台电脑是否连接。

裸地并查集,每次修复后扫一遍N看有没有可以连接的电脑,注意标记电脑能否正常使用。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <iomanip>
#include <vector>
#define maxn 10005
using namespace std;
int f[maxn];
int visit[maxn];
struct node
{
	int x;
	int y;
}a[maxn];
int n;
int d;
double dis(node a,node b)
{
	return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
} 
int find(int x)
{
	int r=x;
	while(f[r]!=r)
	{
		r=f[r];
	}
	return r;
}
void unio(int x,int y)
{
	int fx,fy;
	fx=find(x);
	fy=find(y);
	if(fx!=fy)
	{
		f[fx]=fy;
	}
}
int main()
{
	char s[10];
	int p,q;
	memset(visit,0,sizeof(visit));
	scanf("%d%d",&n,&d);
	for(int i=1;i<=n;i++)
	{
		f[i]=i;
	}
	for(int i=1;i<=n;i++)
	{
		scanf("%d %d",&a[i].x,&a[i].y);
	}
	while(~scanf("%s",&s))
	{
		if(s[0]=='O')
		{
			scanf("%d",&p);
			visit[p]=1;
			for(int i=1;i<=n;i++)
			{
				if(dis(a[p],a[i])<=d&&visit[p]==1&&visit[i]==1)
				{
					unio(p,i);
				}
			}
		}
		else
		{
			scanf("%d%d",&p,&q);
			if(find(p)==find(q))
			{
				cout<<"SUCCESS"<<endl;
			}
			else
			{
				cout<<"FAIL"<<endl;
			}
		}
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值