poj2236 Wireless Network 并查集

19 篇文章 0 订阅

题目链接:here

PS:第一次自己看英文!!第一次不是百度的题意!!!!

题意:东南亚大地震。。他们电脑全坏了。(我去质量真差,神舟的么)。然后人要过去修。。需要把电脑都联网,但是硬件限制,所以电脑只能在 d 距离内成功联网。。。第一行输入电脑个数n和d的值,接着n行,是n个电脑的坐标。。然后接下来有操作。。。O代表修第几台电脑,S代表检测 两台电脑是否已经联网。。。注意这里要用EOF来结束输出。。。(我在这就卡住了半天。。。)

代码:

#include <iostream>
#include <vector>
#include <cstdio>
#include <cmath>
using namespace std;

const int maxn = 1005;

struct Node
{
	int x, y;
}node[maxn];
int fa[maxn];
int n, d;
vector<int> v;
int num;

int find(int x)
{
	while (x != fa[x]) x = fa[x];
	return x;
}

bool judge(int x, int y)
{
	int xx = abs(node[x].x-node[y].x), yy = abs(node[x].y-node[y].y);
	if (xx*xx + yy*yy <= d*d)
		return true;
	return false;
}

void union_set(int x)
{
	int fax = find(x);
	for (int i=0; i<num; i++)
	{
		int fai = find(v[i]);
		if (fai != fax && judge(x, v[i]))
			fa[fai] = fax;
	}
}

int main()
{
	while (scanf("%d %d", &n, &d) != EOF)
	{
		int i;
		num = 0;
		v.clear();
		for (i=1; i<=n; i++)
		{
			fa[i] = i;
			scanf("%d %d", &node[i].x, &node[i].y);
		}
		getchar();
		char c;
		while (scanf("%c", &c) != EOF)
		{
			if (c == 'O')
			{
				int j;
				scanf("%d", &j);
				union_set(j);
				num ++;
				v.push_back(j);
			}
			else if (c == 'S')
			{
				int a, b;
				scanf("%d %d", &a, &b);
				if (find(a) == find(b)) puts("SUCCESS");
				else puts("FAIL");
			}
			getchar();
		}
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值