POJ2236 Wireless Network

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


Status Accepted
Time 1125ms
Memory 184kB
Length 1420
Lang C++
Submitted
Shared
RemoteRunId 16898692
这道题的意思是说,有N台电脑,可连接半径为d,接下来的2--n+1行,每行是按顺序是每台电脑的坐标。

然后每行输入

O+一个数字---表示要修复的电脑序号,

S+X+Y表示X和Y电脑是否能进行通信。

可以通信 输出SUCCESS  否则输出FALL

解题思路:

首先建立一个结构体,按序号存储电脑的信息。

int fix[1007];用来标记该序号电脑是否被修复
struct number{
	int x,y;// 父节点
	int f;	//父点
	int n;//秩 
}num[1007];
for(int i=0;i<n;i++)
{
	scanf("%d%d",&x,&y);
	num[i].x=x;
	num[i].y=y;
	num[i].f=i;
	num[i].n=1;
	fix[i]=0;
}

用来初始化电脑信息。

1--当输入O+电脑序号时,将该电脑标记为已修复,在这N台电脑里遍历寻找该电脑与其余电脑这两点间距离小于等于   d,并且这台其余电脑已经修复过来的话就进行连接。

连接程序如下:

int find(int i)
{
	while(i!=num[i].f)
		i=num[i].f;
	return i;
}
void Union(int i,int j)
{
	int p=find(i);
	int q=find(j);
	if(p!=q)
	{
		if(num[p].n>=num[q].n)
			num[p].n+=num[q].n,num[q].f=p;
		else
			num[q].n+=num[p].n,num[p].f=q;//加上此处的比较,只是加快了程序 
	}
}
2---当输入S+X+Y时,判断x和y的祖先是否相等,若想等则可以连接成功。否则失败。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std; 
struct number{
	int x,y;// 父节点
	int f;	//父点
	int n;//秩 
}num[1007];
int n,d;
int fix[1007];
char s[4];
int find(int i)
{
	while(i!=num[i].f)
		i=num[i].f;
	return i;
}
void Union(int i,int j)
{
	int p=find(i);
	int q=find(j);
	if(p!=q)
	{
		if(num[p].n>=num[q].n)
			num[p].n+=num[q].n,num[q].f=p;
		else
			num[q].n+=num[p].n,num[p].f=q;//加上此处的比较,只是加快了程序 
	}
}
int main()
{
	int x,y;
	int x1,y1;
	int len,temp;
	while(scanf("%d%d",&n,&d)!=EOF)
	{
		for(int i=0;i<n;i++)
		{
			scanf("%d%d",&x,&y);
			num[i].x=x;
			num[i].y=y;
			num[i].f=i;
			num[i].n=1;
			fix[i]=0;
		}
		getchar();
		while(scanf("%s",s)!=EOF)
		{
			if(s[0]=='O')
			{
				scanf("%d",&temp);
				//cout<<"temp=="<<temp<<endl;
				fix[temp-1]=1;
				x=num[temp-1].x;
				y=num[temp-1].y;
				for(int i=0;i<n;i++)
				{
					if(fix[i]&&i!=temp-1)
					{
						//cout<<"i=="<<i<<" "<<num[i].x<<" " <<num[i].y<<endl;
						x1=abs(x-num[i].x);
						y1=abs(y-num[i].y);
						len=x1*x1+y1*y1;//两点间距离公式 
						//cout<<"len="<<len<<endl;
						if(len<= d*d )
						{
							//cout<<i<<"计算机与"<<temp<<"构树"<<endl; 
							Union(temp-1,i);
						}
							
					}
					
				}
			}
			else
			{
				
				scanf("%d%d",&x1,&y1);
				if(find(x1-1)==find(y1-1))
					printf("SUCCESS\n");
				else
					 printf("FAIL\n");
			}
				
		}	
	}
	return 0;
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值