Wireless Network

Description

An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wireless network with the lap computers, but an unexpected aftershock attacked, all computers in the network were all broken. The
computers are repaired one by one, and the network gradually began to work again. Because of the hardware restricts, each computer can only directly communicate with the computers that are not farther than d meters from it. But every computer can be regarded
as the intermediary of the communication between two other computers, that is to say computer A and computer B can communicate if computer A and computer B can communicate directly or there is a computer C that can communicate with both A and B.

In the process of repairing the network, workers can take two kinds of operations at every moment, repairing a computer, or testing if two computers can communicate. Your job is to answer all the testing operations.

Input

The first line contains two integers N and d (1 <= N <= 1001, 0 <= d <= 20000). Here N is the number of computers, which are numbered from 1 to N, and D is the maximum distance two computers can communicate directly. In the next
N lines, each contains two integers xi, yi (0 <= xi, yi <= 10000), which is the coordinate of N computers. From the (N+1)-th line to the end of input, there are operations, which are carried out one by one. Each line contains an operation in one of following
two formats:

1. "O p" (1 <= p <= N), which means repairing computer p.

2. "S p q" (1 <= p, q <= N), which means testing whether computer p and q can communicate.

The input will not exceed 300000 lines.

Output

For each Testing operation, print "SUCCESS" if the two computers can communicate, or "FAIL" if not.

Sample Input

4 1
0 1
0 2
0 3
0 4
O 1
O 2
O 4
S 1 4
O 3
S 1 4

Sample Output

FAIL
SUCCESS

题解:简单并查集。只需要把修好的和距离小于d的加入同一个集合就行了。

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int pre[1005];
int X[1005];
int Y[1005];
int map[1003][1003];
bool good[1005];

int find(int x)
{
	return x == pre[x] ? x : find(pre[x]);
 } 

int cal(int x1,int y1,int x2,int y2)
{
	return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
}

int main()
{
	int n,d;
	while(scanf("%d%d",&n,&d) != EOF)
	{
		for(int i = 1;i <= n;i++)
		{
			pre[i] = i;
		}
		for(int i = 1;i <= n;i++)
		{
			scanf("%d%d",&X[i],&Y[i]);
			for(int j = 1;j < i;j++)
			{
				int t = cal(X[i],Y[i],X[j],Y[j]);
				map[i][j] = map[j][i] = t;
			}
		}

		memset(good,false,sizeof(good));
		char s[10];
		int u,v;
		d *= d;
		while(scanf("%s",s) != EOF)
		{
			if(s[0] == '#')
			{
				break;
			}
			if(s[0] == 'O')
			{
				scanf("%d",&u);
				good[u] = true;
				for(int i = 1;i <= n;i++)
				{
					if(map[u][i] <= d && good[i])
					{
						int x = find(i);
						int y = find(u);
						if(x != y)
						{
							pre[y] = x;
						}
					}
				}
			}
			else
			{
				scanf("%d%d",&u,&v);
				int x = find(u);
				int y = find(v);
				if(x == y)
				{
					printf("SUCCESS\n");
				}
				else
				{
					printf("FAIL\n");
				}
			}
		}
	}

	return 0;
 } 

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-07-30 03:32:08

Wireless Network的相关文章

Wireless Network Watcher(无线网络监视器)

软件简介: Wireless Network Watcher 是一个可以扫描你的无线网络,并显示当前连接到您的网络的所有计算机和设备的列表的小工具.对于连接到您的网络的每台计算机或设备,将显示以下信息:IP地址.MAC地址.该公司生产的网卡.可选的计算机名称. 图片预览: 下载地址:http://dickmoore.cn/Down/wnetwatcher.zip

POJ 2236 Wireless Network (并查集)

Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 18066   Accepted: 7618 Description An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wireless network with the lap computer

POJ 2236 Wireless Network

Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 18146   Accepted: 7660 Description An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wireless network with the lap computers, but an unexpec

Ubuntu wireless network connection in command line

After installed the the graphical card driver in Ubuntu, the gnome desktop was not working anymore, needs to install and setup the wireless network in command line, herewith the procedure, 1, scan the available wifi network, sudo iwlist scan 2, recei

POJ 2236 Wireless Network ||POJ 1703 Find them, Catch them 并查集

POJ 2236 Wireless Network http://poj.org/problem?id=2236 题目大意: 给你N台损坏的电脑坐标,这些电脑只能与不超过距离d的电脑通信,但如果x和y均能C通信,则x和y可以通信.现在给出若干个操作, O p 代表修复编号为p的电脑 S p q代表询问p和q是不是能通信. 思路: 并查集即可.. 如果修复了一台电脑,则把与它相连距离不超过d的且修复了的放在一个集合里面. #include<cstdio> #include<cstring&

Top 10 Free Wireless Network hacking/monitoring tools for ethical hackers and businesses

There are lots of free tools available online to get easy access to the WiFi networks intended to help the network admins and the programmers working on the WiFi systems and we at Team Techworm have picked the top 10 of those for ethical hackers, pro

POJ2236 Wireless Network 并查集 好题

Wireless Network Description An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wireless network with the lap computers, but an unexpected aftershock attacked, all computers in the network were all broke

poj 2236 Wireless Network 【并查集】

Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 16832   Accepted: 7068 Description An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wireless network with the lap computer

poj2236 Wireless Network 并查集简单应用

Description An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wireless network with the lap computers, but an unexpected aftershock attacked, all computers in the network were all broken. The computers

poj2236(Wireless Network)并查集

Description An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wireless network with the lap computers, but an unexpected aftershock attacked, all computers in the network were all broken. The computers