HPUOJ---2017寒假作业--专题1/B-Wireless Network(并查集)

B - 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
思路:将修好的距离小于d的电脑合并,两个被修好的且根节点一样的电脑才能联系。
 
  
 
  
 
  
 
  
#include
#include
#define max 1001+11
int x[max];
int y[max];
int f[max];
int e[max];
int k[max];
int find(int x)
{
	int r=x;
	while(r!=f[r])
	    r=f[r];
	int i=x,j;
	while(i!=r)     //路径压缩 
	{
		j=f[i];
		f[i]=r;
		i=j;
	}
	return r;
}            //找到根节点 
void join(int x,int y)
 {
 	int fx,fy;
 	fx=find(x);
 	fy=find(y);
 	if(fx!=fy)
 	    f[fy]=fx;
 }         //顺其自然 
	int main()
	{
		int n,k,p,q,i,j,l,d;
		char s[6];
		while(~scanf("%d%d",&n,&d))
		{
			
			memset(e,0,sizeof(e));
			for(i=1;i<=n;i++)
			    f[i]=i;      //赋初值 
			for(i=1;i<=n;i++)
			    scanf("%d%d",&x[i],&y[i]);
			while(~scanf("%s",s))
			{
				if(s[0]=='O')    //输入的明明是一个字符,为啥要用字符串呢,那是因为这个字符是每组输入时要第一个输入的,每轮输入结束后还要输入换行符,如果用字符的话,就会把换行符当作下一轮要输入的字符,显然错误。 
				{
					scanf("%d",&k);//字符o表示维修电脑, 
					e[k]=1;//修好的电脑变成1咯,来和坏的分开 
				    for(i=1;i<=n;i++)
				    {
				        l=(x[k]-x[i])*(x[k]-x[i])+(y[k]-y[i])*(y[k]-y[i]);//这个修好的电脑要和其他所有的电脑算一下距离,距离小于d的才可能合并在一起哟。 
						if(l<=d*d&&e[i])//两台距离小于d而且都被修好的电脑才能合并的哟 
						    join(k,i);
					}	
				}
				else if(s[0]=='S')//字符S判断是否能够交流了, 
				{
					scanf("%d%d",&p,&q);
					if(find(p)==find(q)&&e[p]&&e[q])//两台电脑都修好了,而且根节点一样了,那就OK了。 
					    printf("SUCCESS\n");
					else
					    printf("FAIL\n");
				}
			}
		}
		return 0;
    } 

你可能感兴趣的:(并查集)