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

题意描述:

共n台坏了的计算机给出了n台计算机的坐标,维修工人进行维修和检测,‘O’为维修某台计算机‘S’为检修两台计算机是否能通信,能否通信取决于两台计算机间的距离及两台计算机是否已维修好,若两台计算机都可与另一台通信则表示这两台计算机也可以通信。

输出每次检修的结果。

解题思路:
求出计算机间的距离,存入map数组中,因每个计算机都是坏的初始化为-1,维修好后祖先为自己,将能与维修好后计算机通信的其他计算机存为一个祖先,每次检修判断两台计算机是否都维修好并且是否为一个祖先。

#include<stdio.h>
#include<string.h>
#include<math.h>
int f[1010];
double map[1010][1010];
int getf(int i)
{
	if(f[i]==i)
		return i;
	else
	{
		f[i]=getf(f[i]);
		return f[i];
	}
}
int merge(int u,int v)
{
	int t1,t2;
	t1=getf(u);
	t2=getf(v);
	if(t1!=t2)
	{
		f[t2]=t1;
		return 1;
	}
	return 0;
}
int main()
{
	int n,d,x[1010],y[1010],i,j,cut,a,b;
	char s;
	while(scanf("%d%d",&n,&d)!=EOF)
	{
		for(i=1;i<=n;i++)
			scanf("%d%d",&x[i],&y[i]);
		memset(map,0,sizeof(map));
		for(i=1;i<n;i++)
			for(j=i+1;j<=n;j++)
			{
				map[i][j]=(double)(sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j])));
				map[j][i]=map[i][j];
			}
		cut=1;
		memset(f,-1,sizeof(f));
		getchar();
		while(scanf("%c",&s)!=EOF)
		{
			if(s=='O')
			{
				scanf("%d",&a);
				getchar();
				f[a]=a;
				for(i=1;i<=n;i++)
				{
					if(map[i][a]<=d&&f[i]!=-1)
						merge(i,a);//将能够通信的存为一个祖先 
				}
			}	
			else if(s=='S')
			{
				scanf("%d%d",&a,&b);
				getchar();
				if(f[a]!=-1&&f[b]!=-1&&getf(a)==getf(b))
					printf("SUCCESS\n");
				else
					printf("FAIL\n");
			}
		}
	}
	return 0;
}