POJ2236 WirelessNetWork(并查集)

2 篇文章 0 订阅

题目链接
此题是并查集的简单应用,难度不大。(没花太多时间就看到Accepted的感觉好爽,哈哈哈)
懒得写其他的东西了。

#include <iostream>
#include <cstdio>
#include <cstring>
#define MAX 1010
using namespace std;
int N, D;
struct Coordinate
{
	int Xi;
	int Yi;
}Co[MAX];
bool Link(int x, int y)
{
	if ((Co[x].Xi - Co[y].Xi)*(Co[x].Xi - Co[y].Xi) + (Co[x].Yi - Co[y].Yi)*(Co[x].Yi - Co[y].Yi) <= D * D)
		return true;
	return false;
}
bool Work[MAX];
int father[MAX];
int Find(int x)
{
	int r = x;
	while (father[r] != r)
		r = father[r];
	//压缩路径
	int i = x;
	int j;
	while (father[i] != r)
	{
		j = father[i];
		father[i] = r;
		i = j;
	}
	return r;
}
void Union(int x, int y)
{
	int fx = Find(x);
	int fy = Find(y);
	if (fx != fy)
		father[fx] = fy;
}
int main()
{
	memset(Work, 0, sizeof(Work));
	cin >> N >> D;
	for (int i = 0; i<=N; i++)
		father[i] = i;
	char Op;
	int a, b;
	int c;
	for (int i = 1; i<=N; i++)
	{
		scanf("%d%d", &Co[i].Xi, &Co[i].Yi);
	}
	while (cin>>Op)
	{

		if (Op == 'O')
		{
			cin >> c;
			Work[c] = true;
			for (int i = 1; i<=N; i++)
			{
				if (i != c)
				{
					if (Work[i] && Link(i, c))
						Union(i, c);
				}
			}
		}
		else
		{
			cin >> a >> b;
			if (Find(a) == Find(b))
				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、付费专栏及课程。

余额充值